Assigning to None (was Re: Question about Python)

Bengt Richter bokr at oz.net
Sun Jul 3 21:52:32 EDT 2005


On Mon, 04 Jul 2005 09:11:19 +1000, Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:

>On Sun, 03 Jul 2005 19:19:05 +0000, Bengt Richter wrote:
>
>> On Sun, 03 Jul 2005 11:47:07 +1000, Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:
>> 
>>>On Fri, 01 Jul 2005 12:59:20 -0400, François Pinard wrote:
>>>
>>>> [Peter Hansen]
>>>>> Mike Meyer wrote:
>>>>> > Yes. I once grabbed an old program that did assignments to None. But
>>>>> > that's always been a bad idea.
>>>> 
>>>>> What was the use case!?
>>>> 
>>>> People used to assign None to itself as a keyword argument in function
>>>> headers.  The goal was to make a local copy of the reference, which was
>>>> then accessed faster than the global thing.
>>>
>>>Can you say "premature optimization is the root of all evil"?
>>>
>>>I'd like to see the profiling that demonstrated that this made a
>>>significant -- or even measurable -- speed-up in anything but the most
>>>unusual cases.
>>>
>> The difference between local and global access is definitely measurable, though
>> there's no reason to use None as the local name if you want to do that kind
>> of optimization (not possible in 2.4+)
>[snip]
>> about 25% longer to get a global (AND bind it locally, which the two timings share)
>> than to do the same for a local, it seems.
>
>Sure. And if you are actually looping over one million bindings to your
>local variable, and doing NOTHING else, you may approach a 25% time
>saving. That is, one of the unusual cases I mentioned.
>
>But in real world usage, the 25% saving in fetching the variable once or
>twice is almost certainly lost in the noise of the rest of your code.
>Saving 25% of 0.0000001 second running time in a function that takes
>0.0001 second in total to run is pointless.
>
>That's why I asked about the profiling. I'd like to see what sort of real
>world function got enough real benefit from setting None=None to make up
>for the misuse of the language.
>
That particular single thing would only account for 250 seconds even if
I processed a billion things on my old clunker, and I wouldn't likely
notice the 4 minutes in the total time, if it was doing much else. But
this is perhaps the least profitable local caching you can do, compared
to hoisting constant expressions out of the loop, such as dotted expressions
involving the same bound methods over and over, or class variable, or module
variables etc. I'm sure I'm not saying anything you don't know ;-) But sometimes
a combination of such things can add up to starting a utility and going for coffee,
or just standing up and stretching ;-)

Also, as mentioned, there is no reason to write None=None when none=None will
do the silly thing. And I agree it is silly either way 99+ % of the time.

Regards,
Bengt Richter



More information about the Python-list mailing list