Questions tagged [timeit]

A Python built-in module for measuring execution time of small code snippets.

A Python built-in module for measuring execution time of small code snippets. It provides a Timer class which can be used to measure execution times.

According to the documentation, it "avoids a number of common traps for measuring execution times".

Documentation: https://docs.python.org/3/library/timeit.html

400 questions
6
votes
1 answer

ipython %timeit "local variable 'a' referenced before assignment"

I am trying to the run the following code but I get a local variable 'a' referenced before assignment. a = [x for x in range(10)] b = [x for x in range(10)] %timeit a+=b The statement works without the %timeit magic. Is there something I am…
mandy
  • 193
  • 2
  • 9
6
votes
1 answer

IPython: How to save timeit values for each iteration

I'm new to Python and trying to plot the computational speeds of two functions. For example, having defining two functions (see below), how can I return the time for each iteration using the timeit function in IPython/Jupyter? def func1(x) : …
TimEckorote
  • 79
  • 2
  • 3
6
votes
4 answers

How to use else inside Python's timeit

I am new to using the timeit module, and I'm having a hard time getting multi-line code snippets to run inside timeit. What works: timeit.timeit(stmt = "if True: print('hi');") What does not work (these all fail to even run): timeit.timeit(stmt =…
Pro Q
  • 4,391
  • 4
  • 43
  • 92
6
votes
2 answers

Python timeit problem

I'm trying to use the timeit module but I don't know how. I have a main: from Foo import Foo if __name__ == '__main__': ... foo = Foo(arg1, arg2) t = Timer("foo.runAlgorithm()") print t.timeit(2) and my Class Foo has a method named as…
Federico Lenzi
  • 1,632
  • 4
  • 18
  • 34
6
votes
2 answers

Tricky Python string literals in passing parameter to timeit.Timer() function

I'm having a hard time with the setup statement in Python's timeit.Timer(stmt, setup_stmt). I appreciate any help to get me out of this tricky problem: So my sniplet looks like this: def compare(string1, string2): # compare 2 strings if…
Martin08
  • 20,990
  • 22
  • 84
  • 93
6
votes
2 answers

How many CPU cycles one addition take?

I want to measure the number of clock cycles it takes to do an addition operation in Python 3. I wrote a program to calculate the average value of the addition operation: from timeit import timeit def test(n): for i in range(n): 1 +…
Arsen
  • 509
  • 1
  • 5
  • 8
6
votes
2 answers

Get average run time from `%timeit` ipython magic

Trying to time different random functions to see fastest way to choose random item from list. %timeit wants to give me "best of 3" fastest times, but because the runs are random, there's high variance in access times (grab from back of list, will be…
nick_eu
  • 3,541
  • 5
  • 24
  • 38
6
votes
2 answers

Importing a local variable in a function into timeit

I need to time the execution of a function across variable amounts of data. def foo(raw_data): preprocessed_data = preprocess_data(raw_data) time = timeit.Timer('module.expensive_func(preprocessed_data)', 'import module').timeit() However,…
EMiller
  • 2,792
  • 4
  • 34
  • 55
6
votes
1 answer

Why timeit doesn't work on my code snippet?

I think these 3 are logically equivalent, returning the set {1, 3, 4}: set(sum(((1, 3), (4,), (1,)), ())) set(sum([[1, 3], [4], [1]], [])) functools.reduce(operator.or_, ({1, 3}, {4}, {1}), set()) But when I try to check the performance of each in…
wim
  • 338,267
  • 99
  • 616
  • 750
6
votes
2 answers

Python timeit module execution confusion

I'm trying to use the timeit module in Python (EDIT: We are using Python 3) to decide between a couple of different code flows. In our code, we have a series of if-statements that test for the existence of a character code in a string, and if it's…
CraigularB
  • 440
  • 4
  • 11
6
votes
5 answers

Python command line argument semicolon-loop error

I was trying out python -mtimeitso I put python -mtimeit "n = 0; while n < 10: pass" Then an invalid syntax error showed up. same with semicolon and for loop. However, when I try semicolon and loop individually. Both worked fine. python -c "for i…
Mike Lee
  • 1,215
  • 3
  • 10
  • 22
5
votes
2 answers

Why does Python's timeit() execute endlessly?

When trying to use the Python built-in module 'timeit' as follows: timeit.Timer('print "hi"').timeit() it prints more than one line; why is that? It keeps printing "hi" endlessly: hi hi hi hi ...
sramij
  • 4,775
  • 5
  • 33
  • 55
5
votes
2 answers

Getting "TypeError: 'module' is not callable" when calculating execution time with timeit

I am trying to calculate the timings of my python code but I keep getting: TypeError - 'module' is not callable import timeit timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) The link I am checking is this -…
Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55
5
votes
1 answer

Why does the ipython magic function `%timeit -n1 code_block` execute `code_block` multiple times?

I am trying to run a particular test multiple times in ipython using the %timeit magic function. For demonstration purposes, I will just use -n1 instead of -n3 here, and use a simple print(1) function. The %%timeit and the %timeit help says the…
alpha_989
  • 4,882
  • 2
  • 37
  • 48
5
votes
1 answer

Proper way to import when using timeit?

I was testing the following code from one of my previous questions (turning a list into a dictionary): single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3'] if __name__ == '__main__': from timeit import Timer print…
zachwill
  • 4,395
  • 3
  • 19
  • 17