Measure running time a routine in Python

I think this helps us a lot for estimation of how long a program runs in Python. When dealing with a huge amount of data, it takes long time. Showing the running time, started time and end time are useful for logging and improving your program. Certainly there are a few of way to do that. In my view, the following bunch of code is pretty simple and understandable.

import time
from time import gmtime, strftime

# *nix system
start_time = time.time()
# Windows system
#start_time = time.clock()
print("Started the job on %s" % strftime("%a, %d %b %Y %X +0000", gmtime()))

# run all your routines since here
main()
test_1()
finish()
# To demonstrate this script, I try to print 100000 integer numbers
for i in range(100000):
    print(i)
print("It took %s seconds" % str(time.time() - start_time))
print("Finished the job on %s" % strftime("%a, %d %b %Y %X +0000", gmtime()))
Compute the running time of your program in Python
Compute the running time of your program in Python
Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.

https://www.itersdesktop.com/author/nvntung/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.