Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What unit of time does timeit return?

I don't know how to interpret the output from Python's timeit.timeit() function. My code is as follows:

import timeit

setup = """
import pydash
list_of_objs = [
    {},
    {'a': 1, 'b': 2, 0: 0},
    {'a': 1, 'c': 1, 'p': lambda x: x}
]
"""
print(timeit.timeit("pydash.filter_(list_of_objs, {'a': 1})", setup=setup))

The output from this is 11.85382745500101. How do I interpret this number?

like image 214
nivix zixer Avatar asked Aug 17 '15 20:08

nivix zixer


People also ask

What value does Timeit return?

timeit() runs the setup statement one time, then calls the main statement count times. It returns a single floating point value representing the cumulative amount of time spent running the main statement.

Does Timeit return in seconds?

timeit() Function. timeit() is a method of the Python timeit module, and it returns the execution time of the code snippet in seconds.

What does Timeit measure?

Source code: Lib/timeit.py. 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.

What is setup in Timeit?

setup which is the code that you run before running the stmt; it defaults to 'pass'. We generally use this to import the required modules for our code. timer which is a timeit.


1 Answers

The return value is seconds as a float.

It is the total time taken to run the test (not counting the setup), so the average time per test is that number divided by the number argument, which defaults to 1 million.

See the Time.timeit() documentation:

Time number executions of the main statement. This executes the setup statement once, and then returns the time it takes to execute the main statement a number of times, measured in seconds as a float. The argument is the number of times through the loop, defaulting to one million.

like image 80
Martijn Pieters Avatar answered Oct 12 '22 10:10

Martijn Pieters