daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 56 in 22 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from time import time
  2.  
  3. mx = 4500 # 5500 # max you can reach with your computer...
  4.  
  5. t = time()  # start time for the for loop
  6. dmloop = []
  7. for a in range(1, mx):
  8.     for b in range(a, mx):
  9.         dmloop.append(divmod(a, b))
  10. print('for loop: {:.4f} s'.format(time() - t))  # elapsed time
  11.  
  12. t = time()  # start time for the list comprehension
  13. dmlist = [
  14.     divmod(a, b) for a in range(1, mx) for b in range(a, mx)]
  15. print('list comprehension: {:.4f} s'.format(time() - t))
  16.  
  17.  
  18. t = time()  # start time for the generator expression
  19. dmgen = list(
  20.     divmod(a, b) for a in range(1, mx) for b in range(a, mx))
  21. print('generator expression: {:.4f} s'.format(time() - t))
  22.  
  23. # verify correctness of results and number of items in each list
  24. print(dmloop == dmlist == dmgen, len(dmloop))
RAW Paste Data
Top