Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from collections import defaultdict as dd
- import time
- SIZE = 1000
- CYCLES = 10000
- def ddTest():
- d = dd(lambda:'NA')
- for x in xrange(SIZE):
- d[x]
- return d
- def dTest():
- d = {}
- for x in xrange(SIZE):
- d[x] = d.get(x, 'NA')
- return d
- def ddFreq():
- freq = dd(int)
- for x in xrange(SIZE):
- freq[x%7] += 1
- return freq
- def dFreq1():
- freq = {}
- for x in xrange(SIZE):
- m = x%7
- freq[m] = freq.get(m, 0) + 1
- return freq
- def dFreq2():
- freq = {}
- for x in xrange(SIZE):
- freq[x%7] = freq.get(x%7, 0) + 1
- return freq
- def dFreq3():
- freq = {}
- for x in xrange(SIZE):
- m = x%7
- if m in freq:
- freq[m] += 1
- else:
- freq[m] = 1
- return freq
- def tryList():
- freq = [0 for x in xrange(7)]
- for x in xrange(SIZE):
- freq[x%7] += 1
- return freq
- def test(f):
- tStart = time.time()
- for x in xrange(CYCLES):
- result = f()
- tElapsed = time.time() - tStart
- print ("%8s took %.3f seconds and returned a %s with %s items" %
- (f.__name__, tElapsed, type(result), len(result)))
- return result
- if __name__ == '__main__':
- test(ddTest)
- test(dTest)
- test(ddFreq)
- test(dFreq1)
- test(dFreq2)
- test(dFreq3)
- test(tryList)
- -------------------------------------------------
- Output using Python 2.7.2 on seven-year-old MBP:
- ddTest took 5.866 seconds and returned a <type 'collections.defaultdict'> with 1000 items
- dTest took 3.616 seconds and returned a <type 'dict'> with 1000 items
- ddFreq took 3.087 seconds and returned a <type 'collections.defaultdict'> with 7 items
- dFreq1 took 5.115 seconds and returned a <type 'dict'> with 7 items
- dFreq2 took 5.446 seconds and returned a <type 'dict'> with 7 items
- dFreq3 took 3.827 seconds and returned a <type 'dict'> with 7 items
- tryList took 2.432 seconds and returned a <type 'list'> with 7 items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement