Vic*_*ira 6 python optimization performance runge-kutta
有落实经典龙格-库塔方案Python中显示的两种方式在这里.第一个使用lambda函数,第二个没有它们.
哪一个会更快,为什么呢?
如果您使用实现尾部调用优化的Coconut转译器对代码进行预处理,那么它们是完全等效的(与未处理的更快版本一样快),因此您可以使用对您来说更方便的样式。
# Save berna1111's code as rk4.coco; no modifications necessary.
$ coconut --target 3 rk4.coco & python3 rk4.py
50007 function calls in 0.055 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.097 0.097 <string>:1(<module>)
40000 0.038 0.000 0.038 0.000 rk4.py:243(f)
1 0.000 0.000 0.000 0.000 rk4.py:246(RK4)
10000 0.007 0.000 0.088 0.000 rk4.py:247(<lambda>)
1 0.010 0.010 0.097 0.097 rk4.py:250(test_RK4)
1 0.000 0.000 0.097 0.097 {built-in method builtins.exec}
2 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.empty}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
50006 function calls in 0.057 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.057 0.057 <string>:1(<module>)
40000 0.030 0.000 0.030 0.000 rk4.py:243(f)
10000 0.019 0.000 0.049 0.000 rk4.py:265(rk4_step)
1 0.007 0.007 0.057 0.057 rk4.py:273(test_rk4)
1 0.000 0.000 0.057 0.057 {built-in method builtins.exec}
2 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.empty}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Run Code Online (Sandbox Code Playgroud)