Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accurately measure time python function takes

I need to measure the time certain parts of my program take (not for debugging but as a feature in the output). Accuracy is important because the total time will be a fraction of a second.

I was going to use the time module when I came across timeit, which claims to avoid a number of common traps for measuring execution times. Unfortunately it has an awful interface, taking a string as input which it then eval's.

So, do I need to use this module to measure time accurately, or will time suffice? And what are the pitfalls it refers to?

Thanks

like image 328
hoju Avatar asked Nov 06 '09 03:11

hoju


People also ask

How do you precisely measure time in python?

clock() has 1/100th of a second granularity and time. time() is much more precise. On either platform, the default timer functions measure wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing ...

What does time time () do in python?

time() The time() function returns the number of seconds passed since epoch. For Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).

How do you find the performance of a function in python?

We should measure the performance of blocks of python code in a project by recording the execution time and by finding the amount of memory being used by the block. This will help us to know the size of the system required to run the application and also get an idea of the duration of the run.

How do you calculate execution time?

1) Create a loop around whatneeds to be measured, that executes 10, 100, or 1000 times or more. Measure execution time to the nearest 10 msec. Then divide that time bythe number of times the loop executed. If the loop executed 1000 timesusing a 10 msec clock, you obtain a resolution of 10 µsec for theloop.

How to measure time taken by program to execute in Python?

Python – Measure time taken by program to execute 1 Store the starting time before the first line of the program executes. 2 Store the ending time after the last line of the program executes. 3 Print the difference between start time and end time. More ...

What is the use of clock in Python?

The clock function in the time module of Python returns processing time as an output to the end-user. The main role of the function is to facilitate benchmarking and performance testing. The function provides the accurate or correct time taken by the Python code segment to complete its execution.

What is system time in Python?

System time represents a computer system's notion of the passing of time. One should remember that the system clock could be modified by the operating system, thus modifying the system time. Python's time module provides various time-related functions.

What are time-related functions in Python?

Python's time module provides various time-related functions. Since most of the time functions call platform-specific C library functions with the same name, the semantics of these functions are platform-dependent.


2 Answers

According to the Python documentation, it has to do with the accuracy of the time function in different operating systems:

The default timer function is platform dependent. On Windows, time.clock() has microsecond granularity but time.time()‘s granularity is 1/60th of a second; on Unix, time.clock() has 1/100th of a second granularity and time.time() is much more precise. On either platform, the default timer functions measure wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing ... On Unix, you can use time.clock() to measure CPU time.

To pull directly from timeit.py's code:

if sys.platform == "win32":     # On Windows, the best timer is time.clock()     default_timer = time.clock else:     # On most other platforms the best timer is time.time()     default_timer = time.time 

In addition, it deals directly with setting up the runtime code for you. If you use time you have to do it yourself. This, of course saves you time

Timeit's setup:

def inner(_it, _timer):     #Your setup code     %(setup)s     _t0 = _timer()     for _i in _it:         #The code you want to time         %(stmt)s     _t1 = _timer()     return _t1 - _t0 

Python 3:

Since Python 3.3 you can use time.perf_counter() (system-wide timing) or time.process_time() (process-wide timing), just the way you used to use time.clock():

from time import process_time  t = process_time() #do some stuff elapsed_time = process_time() - t 

The new function process_time will not include time elapsed during sleep.

Python 3.7+:

Since Python 3.7 you can also use process_time_ns() which is similar to process_time()but returns time in nanoseconds.

like image 62
Sean Vieira Avatar answered Oct 13 '22 22:10

Sean Vieira


You could build a timing context (see PEP 343) to measure blocks of code pretty easily.

from __future__ import with_statement import time  class Timer(object):     def __enter__(self):         self.__start = time.time()      def __exit__(self, type, value, traceback):         # Error handling here         self.__finish = time.time()      def duration_in_seconds(self):         return self.__finish - self.__start  timer = Timer()  with timer:     # Whatever you want to measure goes here     time.sleep(2)  print timer.duration_in_seconds()     
like image 20
Corey Porter Avatar answered Oct 13 '22 21:10

Corey Porter