The answer to whether you can pass the name of a function you have defined to timeit
is yes. Copying-pasting from the documentation:
To give the timeit module access to functions you define, you can pass a
setup parameter which contains an import statement:
def test():
"""Stupid test function"""
L = [i for i in range(100)]
if __name__ == '__main__':
import timeit
print(timeit.timeit("test()", setup="from __main__ import test"))
Depending on your needs, you may:
- a. want to take a look at the note in the documentation about
timeit()
temporarily turning off garbage collection during the timing (and how to re-enable it) and,
- b. consider using a time decorator, you can find many examples online (see, for instance, Python time measure function or Decorator for timeit.timeit method?).