Python equivalence to inline functions or macros

前端 未结 6 2253
天涯浪人
天涯浪人 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:12

    You can try to use lambda:

    abs2 = lambda x : x.real*x.real+x.imag*x.imag
    

    then call it by:

    y = abs2(x)
    

提交回复
热议问题