Does Python optimize low-power functions?
jeanmichel at sequans.com
Fri Dec 6 19:52:05 CET 2013
----- Original Message -----
> The following two functions return the same result:
> But they may be computed in different ways. The first choice can
> accommodate non-integer powers and so it would logically proceed by
> taking a logarithm, multiplying by the power (in this case, 2), and
> then taking the anti-logarithm. But for a trivial value for the
> power like 2, this is clearly a wasteful choice. Just multiply x by
> itself, and skip the expensive log and anti-log steps.
> My question is, what do Python interpreters do with power operators
> where the power is a small constant, like 2? Do they know to take
> the shortcut?
It is probably specific to the interpreter implementation(cython, jython, iron python etc...). You'd better optimize it yourself should you really care about this.
An alternative is to use numpy functions, like numpy.power, they are optimized version of most mathematical functions.
-- IMPORTANT NOTICE:
The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
More information about the Python-list