I am trying to optimise some python code, via testing (timing) various functions using timeit.
I have found that I am getting different speeds depending on whether a variable is a keyword argument or within the function.
That is:
def test_function(A = value()):
#rest of function....
Is returning a different result than:
def test_function():
A = value()
#rest of function ...
I would have figured they would have very similar results – I am guessing I am not understanding / missing something here…
(doing a 10,000 loops for the tests too)
Keyword arguments are evaluated once at function definition time. So in your first example
value()is called exactly once, no matter how often you call the test function. Ifvalue()is expensive-ish this explains the difference in runtime between the two versions.