On Thu, 21 Jun 2012 21:25:04 +1000, John O'Hagan wrote:Since big_calculation() is only called once, the cost of generating that
> Sometimes a function gets called repeatedly with the same expensive
> def some_func(arg, i):
> same_old_arg = big_calculation()
expensive argument is only paid once.
> for i in lots_of_items:Passing that same_old_arg to some_func is cheap. Once you've paid the
> some_func(same_old_arg, i)
cost of producing the value in the first place, there is absolutely no
difference in cost between passing an "expensive" argument and a "cheap"
> A simple case like that looks OK, but it can get messy when groups ofI'm not sure what you're trying to say here. Argument passing is cheap.
> arguments are passed between functions, especially if the arguments are
> used by functions called within the functions they are passed to (by
> other functions!).
Function call overhead is not quite so cheap, but unless you've carefully
profiled your code and determined that the cost of function overhead is
significant, you're better off using ignoring it. Likely the actual
calculation within the function is hundreds or thousands of times more
expensive than the function call itself.
> Maybe that's a code smell, but it can be cleaned up with:Have you actually measured this to see whether it *actually is* faster,
> import functools
or are you just assuming it will be faster?
I expect that at best it will be no faster at all, and quite possibly
> Old news? Thoughts, criticisms, theories?Premature optimization is the root of all evil.
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.