Advertisement
ikizid

optimize.py

Sep 18th, 2023
727
0
15 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | Software | 0 0
  1. import time
  2.  
  3. # Instead of this
  4. my_list = [1, 2, 3, 4, 5]
  5. squared_list = []
  6. start = time.time()
  7. for num in my_list:
  8.     squared_num = num ** 2
  9.     squared_list.append(squared_num)
  10. end = time.time()
  11. print(end - start)
  12.  
  13. # Do this
  14. my_list = [1, 2, 3, 4, 5]
  15. start = time.time()
  16. sqúared_list = [num ** 2 for num in my_list]
  17. end = time.time()
  18. print(end - start)
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement