Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Generator vs functools.partial?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Steven D'Aprano  
View profile  
 More options Jun 21 2012, 8:19 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 21 Jun 2012 12:19:20 GMT
Local: Thurs, Jun 21 2012 8:19 am
Subject: Re: Generator vs functools.partial?

On Thu, 21 Jun 2012 21:25:04 +1000, John O'Hagan wrote:
> Sometimes a function gets called repeatedly with the same expensive
> argument:

> def some_func(arg, i):
>     (do_something with arg and i)

> same_old_arg = big_calculation()

Since big_calculation() is only called once, the cost of generating that
expensive argument is only paid once.

> for i in lots_of_items:
>     some_func(same_old_arg, i)

Passing that same_old_arg to some_func is cheap. Once you've paid the
cost of producing the value in the first place, there is absolutely no
difference in cost between passing an "expensive" argument and a "cheap"
argument.

> A simple case like that looks OK, but it can get messy when groups of
> arguments are passed between functions, especially if the arguments are
> used by functions called within the functions they are passed to (by
> other functions!).

I'm not sure what you're trying to say here. Argument passing is cheap.
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:

> import functools
> some_func = functools.partial(some_func, big_calculation())
> for i in lots_of_items:
>     some_func(i)

Have you actually measured this to see whether it *actually is* faster,
or are you just assuming it will be faster?

I expect that at best it will be no faster at all, and quite possibly
slightly slower. The main reason is that instead of one function call
(calling some_func directly with two arguments) you now have two (partial
function called with one argument, which internally calls some_func with
two). Possibly that internal call is implemented in C and may be a little
faster than if it were implemented in Python bytecode, but still, two
calls can't be faster than one.

[...]

> Old news? Thoughts, criticisms, theories?

Premature optimization is the root of all evil.

--
Steven


 
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.