On Nov 14, 2012, at 5:37 PM, Chris Withers wrote:

> On 14/11/2012 10:11, mar...@v.loewis.de wrote:
>> 
>> Zitat von Chris Withers <ch...@simplistix.co.uk>:
>> 
>>> a_dict = dict(
>>>    x = 1,
>>>    y = 2,
>>>    z = 3,
>>>    ...
>>>    )
>> 
>>> What can we do to speed up the former case?
>> 
>> It should be possible to special-case it. Rather than creating
>> a new dictionary from scratch, one could try to have the new dictionary
>> the same size as the original one, and copy all entries.
> 
> Indeed, Doug, what are your views on this? Also, did you have a real-world 
> example where this speed difference was causing you a problem?

No, not particularly. I noticed people using dict() and wondered what impact it 
might have in a general case.

> 
>> I don't know how much this would gain, though. You still have to
>> create two dictionary objects. For a better speedup, try
>> 
>> def xdict(**kwds):
>>   return kwds
> 
> Hah, good call, this trumps both of the other options:
> 
> $ python2.7 -m timeit -n 1000000 -r 5 -v 
> "{'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7}"
> raw times: 1.45 1.45 1.44 1.45 1.45
> 1000000 loops, best of 5: 1.44 usec per loop
> $ python2.6 -m timeit -n 1000000 -r 5 -v 'dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'
> raw times: 2.37 2.36 2.36 2.37 2.37
> 1000000 loops, best of 5: 2.36 usec per loop$ python2.6 -m timeit -n 1000000 
> -r 5 -v 'def md(**kw): return kw; md(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'
> raw times: 0.548 0.533 0.55 0.577 0.539
> 1000000 loops, best of 5: 0.533 usec per loop
> 
> For the naive observer (ie: me!), why is that?
> 
> Chris
> 
> -- 
> Simplistix - Content Management, Batch Processing & Python Consulting
>           - http://www.simplistix.co.uk

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to