

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I get time of a Python program's execution?
To measure time time of a program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes.
Example
import time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)
Output
This will give the output −
Time elapsed: 0.0009403145040156798
You can also use the timeit module to get proper statistical analysis of a code snippet's execution time. It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows −
Example
def f(x): return x * x import timeit timeit.repeat("for x in range(100): f(x)", "from __main__ import f", number=100000)
Output
This will give the output −
[2.0640320777893066, 2.0876040458679199, 2.0520210266113281]
- Related Questions & Answers
- How do I time a method execution in Java
- How do I get into a Docker container's shell?
- How do I get the 'state' of a Tkinter Checkbutton?
- How can I get a file's permission mask using Python?
- How do I change a MongoDB user's password?
- How do I redraw an image using Python's Matplotlib?
- How do I get the current time zone of MySQL?
- How can I get a file's size in C++?
- How do you get the current figure number in Python's Matplotlib?
- How do I change matplotlib's subplot projection of an existing axis?
- How do I get list of methods in a Python class?
- How do I display real-time graphs in a simple UI for a Python program?
- How do I get the current date and time in JavaScript?
- How do I display the date, like "Aug 5th", using Python's strftime?
- How to get a subset of JavaScript object's properties?
Advertisements