Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # timeit_preserve_unique.py
- s = 'preserve unique'
- from time import clock
- t = sum([[3, 3, 3, 2, 2, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 4, 6, 5, 1, 5, 5, 5, 4, 5, 5, 4, 6, 5, 5, 6, 4, 4, 10, 10, 10, 5, 5, 10, 5, 11, 6, 10, 11, 6, 6, 11, 6, 11, 11, 10, 10, 10, 12, 1, 11, 10, 1, 12, 7, 3, 10, 4, 4, 4, 7, 1, 12, 12, 8, 4, 12, 12, 12, 11, 8, 4, 1, 7, 9, 7, 9, 9, 7, 8, 9, 7, 8, 8, 8, 0, 9, 0, 0, 7, 8, 7, 7, 1, 1, 1] for z in xrange(100)], [])
- print '[3, 2, 1, 4, 5, 6, 10, 11, 12, 7, 8, 9, 0] ***'
- def preserve_unique_a(items):
- m = 'basic'
- for i in xrange(len(items)-1, -1, -1):
- x = items[i]
- if x in items[:i]:
- del items[i]
- return items, m
- def preserve_unique_b(items):
- m = 'with set() [1 of 2]'
- skip = set()
- for i in xrange(len(items)-1, -1, -1):
- x = items[i]
- if x in skip:
- del items[i]
- elif x in items[:i]:
- del items[i]
- else:
- skip.add(x)
- return items, m
- def preserve_unique_c(items):
- m = 'with set() [2 of 2]'
- skip = set()
- pos = 0
- for item in items:
- if item not in skip:
- skip.add(item)
- items[pos] = item
- pos += 1
- del items[pos:]
- return items, m
- def preserve_unique_d(items):
- m = 'with dict()'
- skip = {}
- pos = 0
- for item in items:
- if item not in skip:
- skip[item] = True
- items[pos] = item
- pos += 1
- del items[pos:]
- return items, m
- for fn in 'abcd':
- test = 'preserve_unique_{}(%s)'.format(fn)
- repeats = []
- print
- for tries in range(10):
- z = t[:]
- length = len(z)
- #print len(z)
- start = clock()
- z,m = eval(test%(z))
- stop = clock() - start
- repeats += [stop]
- print s, m
- print 'length =', length
- print z, 'best to {}: {:.5f}'.format(len(repeats), min(repeats))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement