2

I have a question about the timeit module in python, which is used to determine the time it takes for a piece of code to execute.

t = timeit.Timer("foo","from __main__ import foo")
str(t.timeit(1000))

What is the meaning of the argument 1000 in the above code ?

3 Answers 3

4

From the documentation:

Timer.timeit([number=1000000])

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. The main statement, the setup statement and the timer function to be used are passed to the constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

As documented, the number indicates the number of times timeit should execute the specified program.

Since the first few executions can be significantly slower due to caching, and individual execution times may vary wildly, more timing runs (i.e. a higher value) will yield a more precise result, but also take longer.

Comments

1

In the spirit of teaching a man to fish, just ask Python:

>>> import timeit
>>> t=timeit.Timer()
>>> help(t.timeit)
Help on method timeit in module timeit:

timeit(self, number=1000000) method of timeit.Timer instance
    Time 'number' executions of the main statement.

    To be precise, this executes the setup statement once, and
    then returns the time it takes to execute the main statement
    a number of times, as a float measured in seconds.  The
    argument is the number of times through the loop, defaulting
    to one million.  The main statement, the setup statement and
    the timer function to be used are passed to the constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.