fork(1) download
  1. def del_dups(seq):
  2. seen = {}
  3. pos = 0
  4. for item in seq:
  5. if item not in seen:
  6. seen[item] = True
  7. seq[pos] = item
  8. pos += 1
  9. del seq[pos:]
  10.  
  11. def del_dups_set(seq):
  12. seen = set()
  13. seen_add = seen.add
  14. pos = 0
  15. for item in seq:
  16. if item not in seen:
  17. seen_add(item)
  18. seq[pos] = item
  19. pos += 1
  20. del seq[pos:]
  21.  
  22. lst = [8, 8, 9, 9, 7, 15, 15, 2, 20, 13, 2, 24, 6, 11, 7, 12, 4, 10, 18,
  23. 13, 23, 11, 3, 11, 12, 10, 4, 5, 4, 22, 6, 3, 19, 14, 21, 11, 1,
  24. 5, 14, 8, 0, 1, 16, 5, 10, 13, 17, 1, 16, 17, 12, 6, 10, 0, 3, 9,
  25. 9, 3, 7, 7, 6, 6, 7, 5, 14, 18, 12, 19, 2, 8, 9, 0, 8, 4, 5]
  26.  
  27. def measure(func):
  28. from timeit import Timer
  29.  
  30. N = 50000
  31. setup = 'from __main__ import lst, del_dups, del_dups_set'
  32. t = Timer('%s(lst[:])' % (func,), setup)
  33. print("%s: %.2f us" % (func, 1e6*min(t.repeat(number=N))/N,))
  34.  
  35. if __name__=="__main__":
  36. measure('del_dups')
  37. measure('del_dups_set')
  38.  
Success #stdin #stdout 7.16s 6728KB
stdin
Standard input is empty
stdout
del_dups: 24.15 us
del_dups_set: 23.15 us