Example 1: Using time module
Output
52.9 3.600120544433594e-05
In order to calculate the time elapsed in executing a code, the time module can be used.
- Save the timestamp at the beginning of the code
startusingtime(). - Save the timestamp at the end of the code
end. - Find the difference between the end and start, which gives the execution time.
The execution time depends on the system.
Example 2: Using timeit module
Output
52.9 6.355400000000039e-05
Similar to Example 1, we use timer() method from timeit module.
timeit provides the most accurate results.