[ create a new paste ] login | about

Link: http://codepad.org/Y2ZjKZpH    [ raw code | fork ]

Python, pasted on Nov 27:
import timeit

setup = """
import random
random.seed(0)
item_count = 100000
# divide key range by 5 to ensure lots of duplicates
items = dict((i, random.randint(0, item_count/5)) for i in xrange(item_count))
new_values = [random.randint(0, item_count/5) for i in xrange(item_count)]
"""
setitem_update = """
for i in xrange(item_count):
    items[i] = new_values[i]
"""
straight_update = """
for i in xrange(item_count):
    items.update(i = new_values[i])
"""
straight_update_bulk = """
new_stuff = dict(i,new_values[i]) for i in xrange(item_count)
items.updatei(**new_stuff)
"""

print 'setitem_update ', timeit.Timer(setitem_update, setup).timeit(1000)
print 'straight_update ', timeit.Timer(straight_update, setup).timeit(1000)
print 'straight_update_bulk ', timeit.Timer(straight_update, setup).timeit(1000)


Create a new paste based on this one


Comments: