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
12
votes
4 answers

How to time Django queries

I've always used Python's timeit library to time my little Python programs. Now I'm developing a Django app and I was wondering how to time my Django functions, especially queries. For example, I have a def index(request) in my views.py which does a…
chiurox
  • 1,549
  • 3
  • 18
  • 28
12
votes
5 answers

Python: is there a way to import a variable using timeit.timeit()?

Suppose I have some function that takes an array and changes every element to be 0. def function(array): for i in range(0,len(array)): array[i] = 0 return array I want to test how long this function takes to run on a random array, which…
user2821275
12
votes
2 answers

Using semicolons inside timeit

I can't seem to get timeit.timeit to work when I have exceptions in the statement argument passed as string: # after the first and third semicolon, I put 4 spaces timeit.timeit('try:; a=1;except:; pass') This results in: Traceback (most…
max
  • 49,282
  • 56
  • 208
  • 355
11
votes
1 answer

timeit.timeit VS timeit.Timer.repeat - What is the best practice?

I would like to optimize a function 'myfunc()'. I have several ways to write it, and I would like to check the fastest code. For that, we can use the 'timeit' module. But there are several ways to use it. The most obvious, and apparently the most…
F. Varlet
  • 173
  • 1
  • 9
11
votes
2 answers

Python: multiple assignment vs. individual assignment speed

I've been looking to squeeze a little more performance out of my code; recently, while browsing this Python wiki page, I found this claim: Multiple assignment is slower than individual assignment. For example "x,y=a,b" is slower than "x=a;…
11
votes
2 answers

Timeit, NameError: global name is not defined. But I didn't use a global variable

I'd like to measure the execution speed of the following code: def pe1(): l = [] for i in range(1000): if i%3 == 0 or i%5 == 0: l.append(i) print sum(l) I stored this code under pe1m.py . Now I'd like to test the…
Bentley4
  • 10,678
  • 25
  • 83
  • 134
10
votes
1 answer

Decorator for timeit.timeit method?

I'm trying to write a simple time decorator to measure time taken by functions. However the code below is giving our recursion error. What's wrong with it? import timeit def measure(func): def wrapper(): func_name = func.__name__ …
Uchiha Madara
  • 984
  • 5
  • 16
  • 35
10
votes
1 answer

Why is sorting a python list of tuples faster when I explicitly provide the key as the first element?

Sorting a list of tuples (dictionary keys,values pairs where the key is a random string) is faster when I do not explicitly specify that the key should be used (edit: added operator.itemgetter(0) from comment by @Chepner and the key version is now…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
10
votes
2 answers

Use Python's `timeit` from a program but functioning the same way as the command line?

For instance, documentation says: Note however that timeit will automatically determine the number of repetitions only when the command-line interface is used. Is there a way to call it from within a Python script and have the number of…
endolith
  • 25,479
  • 34
  • 128
  • 192
9
votes
1 answer

python's `timeit` doesn't always scale linearly with number?

I'm running Python 2.7.10 on a 16GB, 2.7GHz i5, OSX 10.11.5 machine. I've observed this phenomenon many times in many different types of examples, so the example below, though a bit contrived, is representative. It's just what I happened to be…
gmoss
  • 1,019
  • 5
  • 17
9
votes
5 answers

Python - Timeit within a class

I'm having some real trouble with timing a function from within an instance of a class. I'm not sure I'm going about it the right way (never used timeIt before) and I tried a few variations of the second argument importing things, but no luck.…
DizzyDoo
  • 1,489
  • 6
  • 21
  • 32
9
votes
4 answers

timeit.timeit variable importing in python

I am trying to use timeit.timeit() in order to find how much time it takes to execute a specific line of code. The problem is that this line includes variables and I need to import them somehow, so my question is how? In order to be more clear, the…
pystudent
  • 531
  • 1
  • 5
  • 19
7
votes
2 answers

Python Timeit and “global name ... is not defined”

I have a problem with timit function for code optimization. For example, I writing functions with parameters in a file, let's call it myfunctions.py containing : def func1(X): Y = X+1 return Y and I test this function in a second file…
cedm34
  • 493
  • 1
  • 5
  • 5
7
votes
2 answers

What does timeit gain by turning off garbage collection?

I was reading the code for the timeit module and I noticed this segment: gcold = gc.isenabled() gc.disable() timing = self.inner(it, self.timer) if gcold: gc.enable() This just stores the state of garbage collection (on or off) and then turns…
aaronasterling
  • 68,820
  • 20
  • 127
  • 125
6
votes
1 answer

Best method to measure execution time of a python snippet

I want to compare execution time of two snippets and see which one is faster. So, I want an accurate method to measure execution time of my python snippets. I already tried using time.time(), time.process_time(), time.perf_counter_ns() as well as…
1 2
3
26 27