Guest User

Untitled

a guest
Aug 21st, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. from time import time
  2.  
  3. nmax = 10000
  4.  
  5. x = 1
  6. start = time()
  7. while x < nmax: # 10K = 0.01s, 100K = 0.13s, 1M = 1.7s
  8. y = []
  9. tmp = x * x * x
  10. while tmp > 0:
  11. y.append(tmp % 10)
  12. tmp /= 10
  13. x += 1
  14. print "numerical", time() - start
  15.  
  16. x = 1
  17. start = time()
  18. while x < nmax: # 10K = 0.08s, 100K = 0.8s, 1M = 11s
  19. y = map(int,str(x*x*x))
  20. x += 1
  21. print "map", time() - start
  22.  
  23. x = 1
  24. start = time()
  25. while x < nmax: # 10K = 0.03s, 100K = 0.3s, 1M = 3s
  26. y = [int(ch) for ch in str(x*x*x)]
  27. x += 1
  28. print "comprehension", time() - start
  29.  
  30. x = 1
  31. start = time()
  32. while x < nmax: # 10K = 0.05s, 100K = 0.28s, 1M = 3.4s
  33. y = []
  34. for ch in str(x*x*x):
  35. y.append(int(ch))
  36. x += 1
  37. print "iterative", time() - start
  38.  
  39. x = 1
  40. start = time()
  41. while x < nmax: # 10K = 0.04s, 100K = 0.33s, 1M = 3.8s
  42. y = []
  43. for ch in str(x*x*x):
  44. y += [int(ch)]
  45. x += 1
  46. print "+ list", time() - start
Add Comment
Please, Sign In to add comment