Python
Python vs JavaScript: What's the Difference?
In this tutorial of difference between Python and JavaScript, we will discuss the key differences...
Python timeit() is a method in Python library to measure the execution time taken by the given code snippet. The Python library runs the code statement 1 million times and provides the minimum time taken from the given set of code snippets. Python timeit() is a useful method that helps in checking the performance of the code.
timeit.timeit(stmt, setup,timer, number)
To work with timeit(), we need to import the module, as shown below:
import timeit
Here is a simple example of timeit() function
# testing timeit()
import timeit
print(timeit.timeit('output = 10*5'))
Output:
0.06127880399999999
We have seen a simple example that gives us the execution time of the simple code statement output = 10*5, and the time is taken to execute it is 0.06127880399999999.
There are two you can execute multiple lines of code in timeit.timeit(), using a semicolon or by saving the code enclosed as a string with triple quotes.
Here are examples that show the working of it.
import timeit
print("The time taken is ",timeit.timeit(stmt='a=10;b=10;sum=a+b'))
Output:
The time taken is 0.137031482
import timeit
import_module = "import random"
testcode = '''
def test():
return random.randint(10, 100)
'''
print(timeit.repeat(stmt=testcode, setup=import_module))
Output:
C:\pythontest>python testtimeit.py The time taken is 0.182619178
Here, are 2 important timeit methods
timeit.default_timer() : This will return the default time when executed.
timeit.repeat(stmt, setup, timer, repeat, number) : same as timeit() , but with repeat the timeit() is called the number of times repeat is given.
# testing timeit()
import timeit
import_module = "import random"
testcode = '''
def test():
return random.randint(10, 100)
'''
print(timeit.timeit(stmt=testcode, setup=import_module))
Output:
0.46715912400000004
default_timer() Example
# testing timeit()
import timeit
import random
def test():
return random.randint(10, 100)
starttime = timeit.default_timer()
print("The start time is :",starttime)
test()
print("The time difference is :", timeit.default_timer() - starttime)
Output:
The start time is : 0.220261875 The time difference is : 0.0004737320000000045
# testing timeit()
import timeit
import_module = "import random"
testcode = '''
def test():
return random.randint(10, 100)
'''
print(timeit.repeat(stmt=testcode, setup=import_module, repeat=5))
Output:
[0.43638873, 0.5040939680000001, 0.5069179909999999, 0.3943449330000002, 0.3546886979999999]
timeit.repeat() works similar to timeit.timeit() function, with the only difference it takes in the repeat argument and gives back the execution time in array format with values as per the repeat number.
The syntax to execute your function inside timeit() on the command line is as follows:
python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [code statement ...]
Command line parameters:
C:\pythontest>python -m timeit -s 'text="hello world"' 20000000 loops, best of 5: 13.1 nsec per loop
Another way you can execute inside command line is as shown below:
>>> import timeit
>>> print("The time taken is ",timeit.timeit(stmt='a=10;b=10;sum=a+b'))
The time taken is 0.15048536300000137
>>>
Here are a few reasons why we consider timeit() is the best way to measure execution time.
Timeit()is used to get the execution time taken for the small code given
Parameters used with timeit()
In this tutorial of difference between Python and JavaScript, we will discuss the key differences...
In the last tutorial, we completed our Python installation and setup. It's time to create your...
The python programming language allows you to use multiprocessing or multithreading.In this...
What is Python? Python is a high level object-oriented, programming language. It has built-in data...
How to Use Python Online Compiler Follow the simple steps below to compile and execute Python...
We have prepared the most frequently asked Python Interview Questions and Answers that will help...