Dicts 5x Faster than Sets
Kamilche
klachemin at home.com
Wed Jun 9 15:24:25 EDT 2004
Hm, I just saw the 'sets' feature, and ran some timings to see if I
should use it instead of 'dict' sometimes. I discovered it's 4x slower
in adding, and 6x slower in removing items!
Here's the code, in case you're interested.
from sets import Set
import mytime
'''
Set Slower than Dict
Sample results:
Function Loops Seconds Loops/sec
***********************************************
Set add 1000000 1.391 718907
Set delete 1000000 1.14 877193
Dict add 1000000 0.344 2906975
Dict delete 1000000 0.172 5813955
'''
def test():
tmr = mytime.Timer()
max = 1000000
s = range(max)
print tmr.heading
s1 = Set()
tmr.startit()
for i in s:
s1.add(i)
tmr.stopit(max)
print tmr.results('Set add')
tmr.startit()
for i in s:
s1.remove(i)
tmr.stopit(max)
print tmr.results('Set delete')
tmr.startit()
s2 = {}
for i in s:
s2[i] = i
tmr.stopit(max)
print tmr.results('Dict add')
tmr.startit()
for i in s:
del s2[i]
tmr.stopit(max)
print tmr.results('Dict delete')
test()
More information about the Python-list
mailing list