0

I have a doubt in performance and can't find how python behaves in each of these cases:

def something(object):
    if object== 'something':
        return 1
    else:
        return 0

Option 1:

def something_bigger(list, object):
    total= 0
    for item in list:
        total+= something(object)

def something_bigger2(list, object):
    total= 0
    for item in list:
        total-= something(object)

Option 2:

def something_bigger(list, object):
    total= 0
    something = something(object)
    for item in list:
        total+= something(object)

def something_bigger2(list, object):
    total= 0
    something = something(object)
    for item in list:
        total-= something(object)

The examples are simple and make no sense but my objective is to understand if python understands that `something()returns always the same and doesn't repeat the if endless times.

Option two, without understanding how python will do this, seems faster but on the other side I'm repeating my code!

mx0
  • 6,445
  • 12
  • 49
  • 54
NBajanca
  • 3,544
  • 3
  • 26
  • 53
  • 1
    That depends on the specific interpreter and how good it is at recognising and optimising these specific cases. Fundamentally you *are* telling Python to repeat the `if..else` every single time. – deceze Jul 15 '16 at 13:55
  • 3
    No, Python interpreter won't infer that it can avoid branching (because it _does not know_ that branching may be avoided). You know it, so write code accordingly. – Łukasz Rogalski Jul 15 '16 at 14:00
  • So the best way is to call methods with imutable variables outside the for? – NBajanca Jul 15 '16 at 14:03
  • 1
    CPython don't optimize your code, PyPy and Pyston tries to do it. If you can avoid multiple `something` calls then do it. – Arnial Jul 15 '16 at 14:09
  • Usually when in doubt about performance, you profile your code. Try cProfile ore line_profiler. – boardrider Jul 17 '16 at 14:50

1 Answers1

0

No. Python is not compiled the way Java or C# applications (for example) are. A line of code is only evaluated right as it's executed.

This question I asked a few weeks ago can really detail one of the core differences in Python: Why doesn't Python spot errors before execution?

That's a different question, sure, but the jist is the same. Python doesn't even know what the next instruction will be. C or C++, on the other hand, are compiled down to a list of assembly instructions.

That's why you need an interpreter to run the scripts.

What you're describing is branch prediction. There's a really excellent explanation on this famous question:

Why is it faster to process a sorted array than an unsorted array?

But Python doesn't compile down to assembly like C++ does. The instructions are not being fed to the CPU in a raw enough format to make branch prediction consistent. You have to detect that behavior yourself and correct it.

Community
  • 1
  • 1
Athena
  • 3,200
  • 3
  • 27
  • 35