mardi 18 avril 2017

Faster "if" statements

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!

Aucun commentaire:

Enregistrer un commentaire