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
--
http://mail.python.org/mailman/listinfo/python-list