In pondering optimization of code, I was wondering which was more expensive in python:
if x:
d = 1
else:
d = 2
or
d = 2
if x:
d = 1
Any thoughts? I like the reduced line count in the second but wondered if reassignment was more costly than the condition switching.
Don’t ponder, don’t wonder, measure — with
timeitat the shell command line (by far the best, simplest way to use it!). Python 2.5.4 on Mac OSX 10.5 on a laptop…:so you see: the “just-if” form can save 1.4 nanoseconds when x is false, but costs 40.2 nanoseconds when x is true, compared with the “if/else” form; so, in a micro-optimization context, you should use the former only if x is 30 times more likely to be false than true, or thereabouts. Also:
…the ternary operator of the if/else has its own miniscule pluses and minuses.
When the differences are as tiny as this, you should measure repeatedly, establish what the noise level is, and ensure you’re not taking differences “in the noise” as significant. For example, to compare statement vs expression if/else in the “x is true” case, repeat each a few times:
now you can state that the expression forms takes (on this machine and versions of key software) 74.2 to 76.0 nanoseconds — the range is much more expressive than any single number would be. And similarly:
now you can state confidently that the statement form takes (under identical conditions) 67.9 to 69.2 nanoseconds; so its advantage, for x true, wrt the expression form, is 4.8 to 8.1 nanoseconds (it’s quite fair to restrict this latter interval estimation to 6.3 to 6.8 nanoseconds, comparing min/min and max/max instead of min/max and max/min as the wider, more prudential estimate does).
How much time and energy is worth devoting to such microscopic differences, once you’ve realized for any given care that they are microscopic, is, of course, a different issue.