Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32365 > unrolled thread

OrderedDict / DIctComprehension

Started byChristian <mining.facts@googlemail.com>
First post2012-10-29 05:36 -0700
Last post2012-10-29 12:03 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  OrderedDict / DIctComprehension Christian <mining.facts@googlemail.com> - 2012-10-29 05:36 -0700
    Re: OrderedDict / DIctComprehension Terry Reedy <tjreedy@udel.edu> - 2012-10-29 13:15 -0400
    Re: OrderedDict / DIctComprehension Christian <mining.facts@googlemail.com> - 2012-10-29 12:03 -0700

#32365 — OrderedDict / DIctComprehension

FromChristian <mining.facts@googlemail.com>
Date2012-10-29 05:36 -0700
SubjectOrderedDict / DIctComprehension
Message-ID<b8a2f8fa-c53d-413b-b3df-5883d075a012@googlegroups.com>
Hi,

is there a way building an OrderedDict faster?

Thanks in advance
Christian

@timeit
def ordered(n=100000):
    d = OrderedDict()
    for i in xrange(n):
        d['key'+str(i)] = i
    return d


@timeit
def comprehension(n=100000):
    d = { 'key'+str(i):i for i in xrange(n) }
    return d


ordered()
comprehension()

'ordered' ((), {}) 0.724609 sec
'comprehension' ((), {}) 0.098318 sec

[toc] | [next] | [standalone]


#32406

FromTerry Reedy <tjreedy@udel.edu>
Date2012-10-29 13:15 -0400
Message-ID<mailman.3034.1351530986.27098.python-list@python.org>
In reply to#32365
On 10/29/2012 8:36 AM, Christian wrote:
> Hi,
>
> is there a way building an OrderedDict faster?
>
> Thanks in advance
> Christian
>
> @timeit
> def ordered(n=100000):
>      d = OrderedDict()
>      for i in xrange(n):
>          d['key'+str(i)] = i
>      return d

try d = OrderedDict(['key'+str(i),i for i in xrange(n)])
In Py3, just range(n). Also
d = OrderedDict(('key'+str(i)) for i in range(n))
may be faster or slower.

>
> @timeit
> def comprehension(n=100000):
>      d = { 'key'+str(i):i for i in xrange(n) }
>      return d
>
>
> ordered()
> comprehension()
>
> 'ordered' ((), {}) 0.724609 sec
> 'comprehension' ((), {}) 0.098318 sec
>


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#32416

FromChristian <mining.facts@googlemail.com>
Date2012-10-29 12:03 -0700
Message-ID<7e12e7ef-5a61-4418-bbc1-95bb92536d9b@googlegroups.com>
In reply to#32365
Too bad that's not (using python2.7)
'ordered_dict_generator' ((), {}) 1.089588 sec

Anyway thanks for your hint!

> Hi,
> 
> 
> 
> is there a way building an OrderedDict faster?
> 
> 
> 
> Thanks in advance
> 
> Christian
> 
> 
> 
> @timeit
> 
> def ordered(n=100000):
> 
>     d = OrderedDict()
> 
>     for i in xrange(n):
> 
>         d['key'+str(i)] = i
> 
>     return d
> 
> 
> 
> 
> 
> @timeit
> 
> def comprehension(n=100000):
> 
>     d = { 'key'+str(i):i for i in xrange(n) }
> 
>     return d
> 
> 
> 
> 
> 
> ordered()
> 
> comprehension()
> 
> 
> 
> 'ordered' ((), {}) 0.724609 sec
> 
> 'comprehension' ((), {}) 0.098318 sec

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web