I just realized that doing
x.real*x.real+x.imag*x.imag
is three times faster than doing
abs(x)**2
where x
Not exactly what the OP has asked for, but close:
Inliner inlines Python function calls. Proof of concept for this blog post
from inliner import inline @inline def add_stuff(x, y): return x + y def add_lots_of_numbers(): results = [] for i in xrange(10): results.append(add_stuff(i, i+1))In the above code the add_lots_of_numbers function is converted into this:
def add_lots_of_numbers(): results = [] for i in xrange(10): results.append(i + i + 1)
Also anyone interested in this question and the complications involved in implementing such optimizer in CPython, might also want to have a look at: