Python equivalence to inline functions or macros

前端 未结 6 2252
天涯浪人
天涯浪人 2020-12-04 15:44

I just realized that doing

x.real*x.real+x.imag*x.imag

is three times faster than doing

abs(x)**2

where x

6条回答
  •  时光取名叫无心
    2020-12-04 16:09

    I'll agree with everyone else that such optimizations will just cause you pain on CPython, that if you care about performance you should consider PyPy (though our NumPy may be too incomplete to be useful). However I'll disagree and say you can care about such optimizations on PyPy, not this one specifically as has been said PyPy does that automatically, but if you know PyPy well you really can tune your code to make PyPy emit the assembly you want, not that you need to almost ever.

提交回复
热议问题