I’m writing a function in Python that I’m planning to run for 10 000 or more times for each script execution. The function currently contains 3 sub-functions but will probably contain 20 or more when the script is complete. I’m just wondering; Will declaring those functions over and over (since the parent function will be run thousands of times) have a recurring performance cost, or is that optimised and not an issue?
Would separating all those sub-functions into a class help with performance?
(I intend to test this and post the results here if nobody knows the answer on the top of their heads.)
The performance impact of a function definition is negligible and comparable to defining a local variable.
The body of the function is compiled only once, all that you end up with during execution of the code-block is loading the compiled block (
LOAD_CONST), and the result of theMAKE FUNCTIONbyte code is then stored in a local variable:Now, if you call that function containing nested functions thousands of times, you do notice a performance impact for that
MAKE_FUNCTIONoperation:Do note that that difference will get smaller the more actual code you put into your functions though. The above examples are very contrived and focus solely on the impact of
MAKE_FUNCTIONbyte code on execution times.Better optimize for readability and maintainability first.