How to Measure Elapsed Time in Python


In this tutorial, we will write an example to show you how to measure elapsed time. In Python, clock() function in time module can be used to get seconds in different time points and the difference between these time points will return elapsed time.

Source Code


#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import time

start = time.clock()
for i in range(1,10):
    print(i)
end = time.clock()
print((end - start).'s elapsed in the loop')

Output:


1
2
3
4
5
6
7
8
9
0.00021599999999999397 second elapsed in the loop

From the output above, we can see that escaped time in the loop is about 0.0002 second. The elapsed time is related to the performance of the machine you have run the program, so you may get a different time if you run the codes in your local machine.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments