fork download
  1. import time
  2. from itertools import product
  3.  
  4. def with_nested_loop():
  5. s = 0
  6. for i in range(5000):
  7. for j in range(5000):
  8. s += i / (j + 1)
  9.  
  10. print(s)
  11.  
  12. def with_product():
  13. s = 0
  14. for i, j in product(range(5000), range(5000)):
  15. s += i / (j + 1)
  16.  
  17. print(s)
  18.  
  19. start = time.time()
  20. with_nested_loop()
  21. elapsed = time.time() - start
  22. print('{:.6f} sec'.format(elapsed))
  23.  
  24. start = time.time()
  25. with_product()
  26. elapsed = time.time() - start
  27. print('{:.6f} sec'.format(elapsed))
  28.  
Success #stdin #stdout 4.24s 9992KB
stdin
Standard input is empty
stdout
113658624.39017837
2.018091 sec
113658624.39017837
2.227822 sec