
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to measure time with high-precision in Python?
To measure time with high precision, 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
Note that different systems will have different accuracy based on their internal clock setup (ticks per second). but it's generally at least under 20ms. Also note that clock returns different things on different platforms. On Unix, it returns the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms. On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.
- Related Articles
- Measure execution time with high precision in C/C++
- How to measure elapsed time in python?
- How to measure elapsed time in nanoseconds with Java?
- How to measure high resistances? (Resistance Measurement Methods)
- How to measure elapsed time in Java?
- How to draw a precision-recall curve with interpolation in Python Matplotlib?
- How to measure the execution time in Golang?
- How to increase precision with division in MySQL?
- How to measure actual MySQL query time?
- How do we measure time?
- Precision Handling in Python
- How to measure time taken by a function in C?
- How to deal with floating point number precision in JavaScript?
- How to measure execution time for a Java method?
- Measure execution time of small Python code snippets (timeit)
