Guest User

python list and deque performance tester

a guest
Jan 28th, 2014
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. """
  2. list and deque performance tester,
  3. Evan @ UC Davis
  4. """"
  5.  
  6. from collections import deque
  7. from timeit import timeit
  8.  
  9. def moveStackList(n):
  10.    list0 = []
  11.    i = 0
  12.    while i < n:
  13.        list0.append(i)
  14.        i += 1
  15.        
  16.    while i > 1:
  17.        list0.pop()
  18.        i -= 1
  19.  
  20. def moveStackDeque(n):
  21.    list0 = deque()
  22.    i = 0
  23.    while i < n:
  24.        list0.append(i)
  25.        i += 1
  26.        
  27.    while i > 1:
  28.        list0.pop()
  29.        i -= 1
  30.        
  31. def moveStackDequeLeft(n):
  32.    list0 = deque()
  33.    i = 0
  34.    while i < n:
  35.        list0.appendleft(i)
  36.        i += 1
  37.        
  38.    while i > 1:
  39.        list0.popleft()
  40.        i -= 1
  41.  
  42. def moveStackDequeHeterogenous(n):
  43.    list0 = deque()
  44.    i = 0
  45.    while i < n:
  46.        list0.appendleft(i)
  47.        i += 1
  48.        
  49.    while i > 1:
  50.        list0.popleft()
  51.        i -= 1
  52.        
  53. if __name__ == "__main__":
  54.    listLength = 5000000
  55.    iterations = 1
  56.    timeListPop = timeit(lambda: moveStackList(listLength), number=iterations)
  57.    timeDequePop = timeit(lambda: moveStackDeque(listLength), number=iterations)
  58.    timeDequeLeft = timeit(lambda: moveStackDequeLeft(listLength), number=iterations)
  59.    timeDequeHetero = timeit(lambda: moveStackDequeHeterogenous(listLength), number=iterations)
  60.  
  61.    print("for len={} and iter = {} list  pop  = {}".format(listLength, iterations, timeListPop))
  62.    print("for len={} and iter = {} deque pop  = {}".format(listLength, iterations, timeDequePop))
  63.    print("for len={} and iter = {} deqye popL = {}".format(listLength, iterations, timeDequeLeft))
  64.    print("for len={} and iter = {} deqye popH = {}".format(listLength, iterations, timeDequeHetero))
Advertisement
Add Comment
Please, Sign In to add comment