PYTHON   17

timing example

Guest on 5th July 2022 05:05:25 PM

  1. #!/usr/bin/python
  2. '''
  3.    File Name: timing_example.py
  4.    Author: Guruansh Singh
  5.    Notes: Demonstrates measurement of time elapsed using time library.
  6. '''
  7.  
  8. import time
  9.  
  10. foo = [None]*1000
  11.  
  12. start = time.time() #Record time at the beginning of the loop
  13.  
  14. for i in range (0,1000):
  15.     foo[i]=i
  16.  
  17. stop = time.time() #Record time at the end of the loop
  18.  
  19. #Note how I did not take any lines below the loop
  20. #Use start and stop immediately before and after
  21.  
  22. timeTaken=(stop-start)*1000; #time.time() gives time in s, so convert to ms
  23.  
  24. timeTaken="{:.4f}".format(timeTaken)
  25.  
  26. s = "Time taken to populate list is " + timeTaken + " milliseconds"
  27. print s

Raw Paste


Login or Register to edit or fork this paste. It's free.