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!

5
  • 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. Commented Jul 15, 2016 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. Commented Jul 15, 2016 at 14:00
  • So the best way is to call methods with imutable variables outside the for? Commented Jul 15, 2016 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. Commented Jul 15, 2016 at 14:09
  • Usually when in doubt about performance, you profile your code. Try cProfile ore line_profiler. Commented Jul 17, 2016 at 14:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.