Python

Python Elapsed Time Measurement

Python elapsed time measurement includes some built-in modules which are used to measure the time which is elapsed during the execution or compilation of any program or code of Python. In Python, many modules can be used to calculate the elapsed time of code execution which includes “time.time()”, “time.process_time()”, “timeit()”, “timeit.repeat()” and “DateTime()”. These are the major modules that can be used in Python to find out the elapsed time of any program execution.

Syntax for Timeit() Function:

The syntax to utilize the “timeit()” module should be made in the function that we created to be called in the time of the print function.

Syntax for Timeit.Repeat() Function:

The syntax of the “timeit.repeat()” function consists of three parameters inside the program function where the “number” parameter refers to the nearby value to be read and the “repeat” parameter performs the iteration of time to be measured.

Syntax for Time.Time() Function:

The syntax of the “time.time()” function looks after the elapsed time by comparing the time at the starting and ending points of the program compilation.

Syntax for Time.Process_Time() Function:

Its syntax is somehow similar to the “time.time()” function but it considers the processing time of the CPU and the end user.

Example 1: Using the Timeit() Function to Measure the Elapsed Time of Lambda Expression

The Python library “timeit()” module is used to measure the elapsed time that could be required by the code snippet for its execution. After running the code statement one million times, it provides the minimal amount of the time requested from the specified set of code snippets by the Python library.

Here, we will explain our Python code snippet.

In the code, we first import the Python library of “timeit” as we are concerned to measure the elapsed time of the Python code. After a while, we create a function named “Execution_Time” and apply the “timeit()” module on the library along with the print function with the message, “We have used the timeit module!”, within the function. Lastly, we call the “Execution_Time” function in the print statement. We give it as “secs” as it gives the time in “seconds” after the implementation of the code.

The snapshot that is previously provided prints the statement that is provided to the function. Lastly, it provides the elapsed time of the program that can be seen in the last line of the output display which is around “38.02” seconds.

Example 2: Using the Timeit.Repeat() Function to Measure the Elapsed Time in Microseconds

In this example, we want to measure the elapsed time of the code snippet in small units such as in microseconds using the “timeit.repeat()” module. The “timeit.repeat()” function has a special feature that if we give this module the default value, it returns a list of the times that the provided code snippet was executed. This approach spares us from having to build a loop and store the values in an array.

Let us discuss the code snippet of where we imported the library of “timeit”. After importing the library, we define a “cube_exponent” function to test the execution time by returning the value. We create a function named “Time_record”, apply the “timeit.repeat()” module, and assign the “lambda” as “cube_exponent(3)” that we used for testing “number” as “13” and the “repeat=4” to make it to only a total of four iterations of the loop.

After that, we create the function of “Execution_time” that stores the “enumerate” elapsed time of the “Time_record” function. To convert the time into the microseconds, we use the “round()” parameter by assigning “10” as the exponent “6”. To display the time in microseconds, we use the parameter of indexes for four cases in the “print()” function along with the “Time Taken” variable stored in it as “micro_seconds”.

In the snapshot of the output display, we can see that we got four cases as we provided the “4” in the “repeat” interval in the code snippet. The first case takes more time as compared to further cases because the first case always needs much efficiency and time of a system for its processing. “Case 1” takes “16.6µs”, “Case 2” takes “13.5µs”, “Case 3” takes “13.3µs” and “Case 4” takes “13.3µs”. This time varies from machine to machine. Every system takes a different time in the processing of the code execution.

Example 3: Using the Time.Time() Function to Measure the Elapsed Time of a Simple Expression

The “time.time()”module gives back the current time as a floating point number in seconds. The time has always been returned as a floating point value. However, certain machines can only provide it with precision up to one second. This technique measures the amount of time that passed by timing the execution of each line of code, subtracting those times, and printing the results on the screen.

Since we are concerned with the measurement of elapsed time, we import the library of “time”. After that, we apply the “time.time()” function on the “Start” before the line of code. We provide our “print()” function to print the product of “10240” and “55.9” just to provide our system with a line of code to perform the execution. To access the time after the code execution, we apply the “time.time()” function on the” Stop” variable. To display the elapsed time between the code execution and rest time, we perform it in the “print()” function. To display the time, we subtract the elapsed time stored in “Start” from the time stored in “Stop”.

Our output display shows the product of “10240” and “55.9” on the first line of the output display which is “572416”. In the second line, it displays the elapsed time which is around “0.001”.

Example 4: Using the Time.Process_Time() Function to Measure the Elapsed Time of the System and User CPU Time

The float value of time in seconds is always returned by the time.process_time() function. It returns the total amount of system and user CPU time used by the existing process, measured in fractional seconds. It eliminates the time spent when the system is in sleeping mode.

This example is very similar to our previous examples. The unique thing in this example is our “time.process_time()” function. We import the “time” library as we imported in the previous examples. Then, we define the “print_twice()” along with the variable “z”. We return the variable here after its iteration along with “54321”. Before the code execution, we apply the “time.process_time()” function on “Start” before the system undergoes execution. Then, we call the “print_twice” with the value provided which is “521”. After the execution of the program, we apply the “time.process_time()” function again on “Stop”. In the end, we apply the “print()” function to display the whole elapsed time consumed for the execution of our code snippet by the user CPU time.

Our output snapshot displays the total elapsed time in microseconds which is “7500.0 ms”. This time depends upon the efficiency and speed of the system. Therefore, this period differs amongst systems.

Conclusion

Python elapsed time measurement is a very easy way to determine the time for execution of any of our program code. For an explanation, we used four different examples to explain the four different modules of Python to measure the elapsed time. In our examples, we discussed the elapsed time measurement in seconds and microseconds. Each illustration is elaborated on to make the technique comprehensive and practically implementable.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.