Example from documentation
def test():
"""Stupid test function"""
L = []
for i in range(100):
L.append(i)
if __name__ == '__main__':
import timeit
print(timeit.timeit("test()", setup="from __main__ import test"))
but how to call a function with parameters, for example, a function like this:
def test(some_object):
"""Stupid test function"""
L = []
for i in range(100):
L.append(some_object)
timeit() function returns the number of seconds it took to execute the code.
This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.
The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.
err, if I get your question right, you're just looking for that?
anobj = 42 # where it can be whatever object
def test(foo):
pass # do something with foo
if __name__ == '__main__':
import timeit
print(timeit.timeit("test(anobj)", setup="from __main__ import test, anobj"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With