Does Python optimize function calls from loops?

后端 未结 5 520
不思量自难忘°
不思量自难忘° 2021-02-04 04:50

Say, I have a code which calls some function millions time from loop and I want the code to be fast:

def outer_function(file):
    for line in file:
        inne         


        
5条回答
  •  梦谈多话
    2021-02-04 05:27

    CPython (the "standard" python implementation) doesn't do this kind of optimization.

    Note however that if you are counting the CPU cycles of a function call then probably for your problem CPython is not the correct tool. If you are 100% sure that the algorithm you are going to use is already the best one (this is the most important thing), and that your computation is really CPU bound then options are for example:

    • Using PyPy instead of CPython
    • using Cython
    • Writing a C++ module and interfacing it with sip
    • If possible implement your algorithm with numpy simd approach
    • If possible move the computation on GPU hardware using for example PyCuda

提交回复
热议问题