[Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Ɓukasz Rekucki
Hi, I posted this (by accident) off the list: On 2012-11-14, at 23:43 , Chris Withers wrote: On 14/11/2012 22:37, Chris Withers wrote: On 14/11/2012 10:11, mar...@v.loewis.de wrote: def xdict(**kwds): return kwds Hah, good call, this trumps both of the other options: 100 -r 5 -v

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Greg Ewing
mar...@v.loewis.de wrote: It's faster than calling dict() because the dict code will create a second dictionary, and discard the keywords dictionary. Perhaps in the case where dict() is called with keyword args only, it could just return the passed-in keyword dictionary instead of creating

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Stefan Behnel
Greg Ewing, 15.11.2012 11:48: mar...@v.loewis.de wrote: It's faster than calling dict() because the dict code will create a second dictionary, and discard the keywords dictionary. Perhaps in the case where dict() is called with keyword args only, it could just return the passed-in keyword

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Terry Reedy
On 11/15/2012 9:58 AM, Stefan Behnel wrote: Greg Ewing, 15.11.2012 11:48: mar...@v.loewis.de wrote: It's faster than calling dict() because the dict code will create a second dictionary, and discard the keywords dictionary. Perhaps in the case where dict() is called with keyword args only,

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Richard Oudkerk
On 15/11/2012 4:21pm, Terry Reedy wrote: I was thinking that CPython could check the ref count of the input keyword dict to determine whether it is newly created and can be returned or is pre-existing and must be copied. But it seems not so. def d(**x): return sys.getrefcount(x) import sys

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Greg Ewing
Stefan Behnel wrote: This should work as long as this still creates a copy of d at some point: d = {...} dict(**d) It will -- the implementation of the function call opcode always creates a new keyword dict for passing to the called function. -- Greg

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-15 Thread Doug Hellmann
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

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-14 Thread Chris Withers
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

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-14 Thread Chris Withers
On 14/11/2012 22:37, Chris Withers wrote: On 14/11/2012 10:11, mar...@v.loewis.de wrote: def xdict(**kwds): return kwds Hah, good call, this trumps both of the other options: $ python2.7 -m timeit -n 100 -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

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-14 Thread Xavier Morel
On 2012-11-14, at 23:43 , Chris Withers wrote: On 14/11/2012 22:37, Chris Withers wrote: On 14/11/2012 10:11, mar...@v.loewis.de wrote: def xdict(**kwds): return kwds Hah, good call, this trumps both of the other options: $ python2.7 -m timeit -n 100 -r 5 -v

Re: [Python-Dev] performance of {} versus dict(), de fmd(**kw): return kw trumps all ; -)

2012-11-14 Thread martin
Zitat von Chris Withers ch...@simplistix.co.uk: $ python2.7 -m timeit -n 100 -r 5 -v {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7} raw times: 1.49 1.49 1.5 1.49 1.48 100 loops, best of 5: 1.48 usec per loop $ python2.7 -m timeit -n 100 -r 5 -v 'dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'