Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32406
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: OrderedDict / DIctComprehension |
| Date | 2012-10-29 13:15 -0400 |
| References | <b8a2f8fa-c53d-413b-b3df-5883d075a012@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3034.1351530986.27098.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web