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 another one?

--
Greg
___
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


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
 dictionary instead of creating another one?

This should work as long as this still creates a copy of d at some point:

d = {...}
dict(**d)

Stefan


___
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


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, it could just return the passed-in keyword
dictionary instead of creating another one?


This should work as long as this still creates a copy of d at some point:

 d = {...}
 dict(**d)


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
 d(a = 3)
2
 d(**{'a': 3})
2
 b = {'a': 3}
 d(**b)
2

I was expecting 3 for the last one.

--
Terry Jan Reedy

___
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


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
  d(a = 3)
2
  d(**{'a': 3})
2
  b = {'a': 3}
  d(**b)
2

I was expecting 3 for the last one.


Isn't it always newly created?

 def f(**x): return x
...
 b = {'a':3}
 b is f(**b)
False

--
Richard

___
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


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
___
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


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 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 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 1.45 1.45
 100 loops, best of 5: 1.44 usec per loop
 $ python2.6 -m timeit -n 100 -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
 100 loops, best of 5: 2.36 usec per loop$ python2.6 -m timeit -n 100 
 -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
 100 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


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 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?



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 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 1.45 1.45
100 loops, best of 5: 1.44 usec per loop
$ python2.6 -m timeit -n 100 -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
100 loops, best of 5: 2.36 usec per loop$ python2.6 -m timeit -n 
100 -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
100 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


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 1.45 1.45
100 loops, best of 5: 1.44 usec per loop
$ python2.6 -m timeit -n 100 -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
100 loops, best of 5: 2.36 usec per loop$ python2.6 -m timeit -n
100 -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
100 loops, best of 5: 0.533 usec per loop


Before anyone shoots me, yes, wrong python for two of them:

$ 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)'
raw times: 2.35 2.36 2.41 2.42 2.35
100 loops, best of 5: 2.35 usec per loop

$ python2.7 -m timeit -n 100 -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.507 0.515 0.516 0.529 0.524
100 loops, best of 5: 0.507 usec per loop

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


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
 {'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
 100 loops, best of 5: 1.44 usec per loop
 $ python2.6 -m timeit -n 100 -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
 100 loops, best of 5: 2.36 usec per loop$ python2.6 -m timeit -n
 100 -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
 100 loops, best of 5: 0.533 usec per loop
 
 Before anyone shoots me, yes, wrong python for two of them:
 
 $ 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)'
 raw times: 2.35 2.36 2.41 2.42 2.35
 100 loops, best of 5: 2.35 usec per loop
 
 $ python2.7 -m timeit -n 100 -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.507 0.515 0.516 0.529 0.524
 100 loops, best of 5: 0.507 usec per loop

The last one is kind-of weird, it seems to be greatly advantaged by the local 
lookup:

 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: 0.676 0.683 0.682 0.698 0.691
100 loops, best of 5: 0.676 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)'

raw times: 1.64 1.66 1.4 1.44 1.44
100 loops, best of 5: 1.4 usec per loop
 python2.7 -m timeit -n 100 -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.188 0.203 0.201 0.195 0.202
100 loops, best of 5: 0.188 usec per loop
 python2.7 -m timeit -n 100 -r 5 -v -s 'def md(**kw): return kw' 
 'md(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'
raw times: 0.871 0.864 0.863 0.889 0.871
100 loops, best of 5: 0.863 usec per loop

___
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


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)'
raw times: 2.35 2.36 2.41 2.42 2.35
100 loops, best of 5: 2.35 usec per loop

$ python2.7 -m timeit -n 100 -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.507 0.515 0.516 0.529 0.524
100 loops, best of 5: 0.507 usec per loop

For the naive observer (ie: me!), why is that?


It's faster than calling dict() because the dict code will
create a second dictionary, and discard the keywords dictionary.

It's (probably) faster than the dictionary display, because
the {} byte code builds the dictionary one-by-one, whereas
the keywords dictionary is built in a single step (taking
all keys and values from the evaluation stack).

Regards,
Martin


___
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