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
37
votes
3 answers

List comprehension vs generator expression's weird timeit results?

I was answering this question, I preferred generator expression here and used this, which I thought would be faster as generator doesn't need to create the whole list first: >>> lis=[['a','b','c'],['d','e','f']] >>> 'd' in (y for x in lis for y in…
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
34
votes
3 answers

IPython %timeit what is loop and iteration in the options?

I am wondering about the %timeit command in IPython From the docs: %timeit [-n -r [-t|-c] -q -p

-o] setup_code Options: -n: execute the given statement times in a loop. If this value is not given, a fitting value is chosen. -r: repeat…

bner341
  • 525
  • 1
  • 7
  • 8
29
votes
2 answers

Can you capture the output of ipython's magic methods? (timeit)

I want to capture and plot the results from 5 or so timeit calls with logarithmically increasing sizes of N to show how methodX() scales with input. So far I have tried: output = %timeit -r 10 results = methodX(N) It does not work... Can't find…
Gus
  • 691
  • 1
  • 8
  • 12
29
votes
2 answers

What do `ns` and `us` stand for in `timeit` result?

I was trying to compare performance of two statements with timeit, and the results are something like: 100 loops, best of 3: 100 ns per loop 100 loops, best of 3: 1.96 us per loop But I don't know what these ns and us stands for, so I don't know…
satoru
  • 31,822
  • 31
  • 91
  • 141
28
votes
3 answers

Inconsistency between %time and %timeit in IPython

I am confronted to a weird situation that I can't explain. Here is my test timing the generation of a large list of tuples: In [1]: def get_list_of_tuples(): ...: return [(i,) for i in range(10**6)] ...: In [2]: %time res =…
badzil
  • 3,440
  • 4
  • 19
  • 27
19
votes
6 answers

timeit ValueError: stmt is neither a string nor callable

I played with timeit in Python, got a weird problem. I define a simple function add. timeit works when I pass add two string parameters. But it raises ValueError: stmt is neither a string nor callable when I pass add two int parameters. >>>…
liang li
  • 253
  • 1
  • 2
  • 9
19
votes
1 answer

Surprising results with Python timeit: Counter() vs defaultdict() vs dict()

I obtained very surprising results with timeit, can someone tell me if I am doing something wrong ? I am using Python 2.7. This is the contents of file speedtest_init.py: import random to_count = [random.randint(0, 100) for r in range(60)] These…
BlueTrin
  • 9,610
  • 12
  • 49
  • 78
19
votes
4 answers

python: slow timeit() function

When I run the code below outside of timeit(), it appears to complete instantaneously. However when I run it within the timeit() function, it takes much longer. Why? >>> import timeit >>> t = timeit.Timer("3**4**5") >>>…
HC.
  • 307
  • 2
  • 6
18
votes
2 answers

Local variables in Python timeit setup

In all of the places I read about timeit I found only that I can use variables in this way: s1 = 'abc' s2 = 'abc' timeit.timeit('s1==s2', 'from __main__ import s1, s2', number=10**4) or s1 = 'abc' s2 = 'abc' def func(): timeit.time('s1==s2',…
pystudent
  • 531
  • 1
  • 5
  • 19
16
votes
2 answers

what is diffrence between number and repeat in python timeit?

I can not understand the difference between number and repeat in timeit library, so would you kindly tell me what is the difference between them?
mark080
  • 389
  • 3
  • 11
15
votes
3 answers

timeit module in python does not recognize numpy module

I want to test the processing time between 2 identical lists, specifically for a normal list and a numpy list. My code is import timeit import numpy as np t = timeit.Timer("range(1000)") print t.timeit() u = timeit.Timer("np.arange(1000)") print…
user3211991
  • 441
  • 2
  • 8
  • 13
15
votes
2 answers

Python timeit and program output

Is there any way to use the timeit function to output both the function result and the time it took to process at the same time? Right now I am using timer = Timer('func()', 'from __main__ import func') print timer.timeit(1) But this just outputs…
MyNameIsKhan
  • 2,594
  • 8
  • 36
  • 56
14
votes
1 answer

Why does python's timeit use the 'best of 3' to measure the time elapsed?

I do not see the rationale why python's timeit module measures the time using the best of 3. Here is an example from my console: ~ python -m timeit 'sum(range(10000))' 10000 loops, best of 3: 119 usec per loop Intuitively, I would have put the…
zell
  • 9,830
  • 10
  • 62
  • 115
14
votes
5 answers

How to use timeit when timing a function

Let me start off by saying I know almost nothing about python but have to write a program in three different languages (already done in java and c++). I need to be able to time the execution of a method a certain number of times and then print the…
Robert Spratlin
  • 305
  • 3
  • 5
  • 8
13
votes
1 answer

%%timeit not returning defined variable

I'm trying to use IPython magic command %%timeit and I run into some problems. The chunk that I'm trying to time is not returning a variable I define in it. Specifically, let's say I want to measure how long does it take to set variable var to…
RadRuss
  • 484
  • 2
  • 6
  • 16
1
2
3
26 27