2

I usually use timeit in jupyter notebook like this:

def some_function():
    for x in range(1000):
    return x

timeit(some_func())

and get the result like this:

6.3 ms ± 42.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

but today I got an error like this:

TypeError                                 Traceback (most recent call last)
<ipython-input-11-fef6a46355f1> in <module>
----> 1 timeit(some_func())

TypeError: 'module' object is not callable

How does it occur?

2
  • 1
    I guess you are running import timeit, then trying to execute the module. Instead, you should import the function. Changing your import statement to from timeit import timeit should fix this. Commented Mar 15, 2020 at 15:23
  • Please provide a minimal reproducible example. Commented Mar 15, 2020 at 16:21

2 Answers 2

1

You are currently trying to execute the timeit module, rather than the function contained within.

You should change your import statement from import timeit to from timeit import timeit. Alternatively, you can call the function using timeit.timeit.

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

2 Comments

I got another error like this ValueError: stmt is neither a string nor callable
That seems unrelated to the question you posted. I recommend that you have a go at fixing it yourself. If you can't, then please open a new question with the relevant information (error traceback and source code).
0

After searching and trying for a while I realize that when we want to use timeit(some_function()), we do not need import timeit but we should write it in another input of jupyter notebook like this:

IN [1]:

def some_function():
    for x in range(1000):
    return x

IN [2]:

timeit(some_func())

and we will get output like this:

280 ns ± 2.78 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

When we write it in one input like this:

IN [1]:

def some_function():
    for x in range(1000):
    return x

timeit(some_func())

we'll get an error timeit not define and when we 'import timeit' we'll got another error like I produce on the question TypeError: 'module' object is not callable.

because when we import timeit we need to specify the stmt and setup (if available) e.g:

import timeit

SETUP = """
    import yourmodul_here
"""

TEST_CODE = """
    def some_function():
    for x in range(1000):
    return x
"""

timeit.timeit(stmt=TEST_CODE, setup=SETUP, number=2000000)

And we'll get the output like this:

0.12415042300017376
  • stmt is code to run
  • setup is something that need to load before TEST_CODE run
  • The stmt will execute as per the number is given here. default = 1000000

so when we import timeit we need to write more I guess.

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.