-1

I have a quick question about what would be faster to compute in python. It's a large simulation, so any little thing to save time helps.

As I understand it, when python evaluates an "if" statement with an "or" in it, it doesn't bother with the second condition if the first condition is true.

Here I want check whether a randomly generated float between 0 and 1 is less than some exponential factor.

if random() < exp(a-b):
   do_things()

So would it be worth it in this case to make logically redundant "if" statements when the second condition implies the first, but the first might be faster to calculate?

if a > b or random() < exp(a-b):
  do_things()

And would this be faster enough to even matter? Any input is appreciated, thanks!

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
  • Why is this tagged `physics`? – Mr. Xcoder Apr 18 '17 at 19:29
  • 5
    you can test execution time with short example based on this method : http://stackoverflow.com/questions/1557571/how-to-get-time-of-a-python-program-execution – Dadep Apr 18 '17 at 19:31
  • 5
    Why not time it and see for yourself? IPython has `%timeit` magic command and Python has `timeit` that you can use: https://docs.python.org/3/library/timeit.html. – Colin Apr 18 '17 at 19:31
  • 5
    Instead of posting... why don't you just measure? (https://docs.python.org/2/library/timeit.html) – thebjorn Apr 18 '17 at 19:31
  • On my computer, the second expression is evaluated ~20% slower when ab. – DYZ Apr 18 '17 at 19:32
  • 3
    "It's a large simulation, so any little thing to save time helps." Maybe it's time to consider not using Python for this section of code? Python is a great language, but if performance is your bottleneck you might need to change your method of computation to something else. – GManNickG Apr 18 '17 at 19:34
  • of course if `a > b` is `False`, it short-circuits the evaluation of the second part, so it's faster than evaluating both conditions, then `or` them. – Jean-François Fabre Apr 18 '17 at 19:35
  • 3
    If you are trying to optimise a larger piece of code, make sure you **profile first**, to make sure you focus your efforts on those areas where it is going to matter. – Martijn Pieters Apr 18 '17 at 19:35
  • Short-circuit evaluation is only better if the time it takes to run the later condition is longer than the time it takes to branch. In this case, that depends on the random number generator, the algorithm for computing `exp`, the values of `a` and `b`, and the branch predictor. [Which is faster?](https://ericlippert.com/2012/12/17/performance-rant/) It doesn't matter. Profile, find your bottlenecks, and then focus your optimization there. – Andrew Apr 18 '17 at 19:38
  • 3
    Martijn is right. Prototype in Python (strive for correctness first), identify bottlenecks by profiling the code, optimize where possible and if necessary rewrite hot loops using something like Cython. – Paulo Scardine Apr 18 '17 at 19:40
  • Yeah, it just occurred after I asked I can time it myself. – Connor Dolan Apr 18 '17 at 20:57

1 Answers1

1

That means for (a and b and c), that b or c would not be tested anymore if a is false.

Similar, if you have an OR expression (a or b) and a is true, b is never evaluated.

So to sum up, the clauses don't fail faster with separation..

Don't worry about performance, worry about readability.... Thank you

Pankur
  • 29
  • 5