What unit of time does timeit return?

29,992

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.

Share:
29,992
nivix zixer
Author by

nivix zixer

Updated on July 09, 2022

Comments

  • nivix zixer
    nivix zixer almost 2 years

    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?

  • Mad Physicist
    Mad Physicist about 6 years
    So this means that it is better to repeat 1e6 times with a number of 1 and take the minimum (implied by the docs)?
  • Martijn Pieters
    Martijn Pieters about 6 years
    @MadPhysicist no. Because then you are enabling and disabling the garbage collector a million times too. Do not set number to 1.