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

Why manual string reverse is worse than slice reverse in Python 2.7? What is the algorithm being used in Slice?

Below the performance difference between Slice and manual reverse operation. If this is the case, What is the reason for that? timeit.timeit("a[::-1]","a=[1,2,3,4,5,6]",number=100) 6.054327968740836e-05 timeit.timeit("[a[i] for i in…
logeekal
  • 525
  • 4
  • 13
5
votes
1 answer

Timeit doesn't work inside of a function

I'm just playing around in python with timeit and the following code works properly: def mysleep(n): import time time.sleep(n) import timeit for k in range (1,5): def mytime(): mysleep(k) t1 = timeit.Timer("mytime();", "from…
Anna
  • 163
  • 1
  • 5
5
votes
2 answers

Speed up loading 24-bit binary data into 16-bit numpy array

I use the following code to load 24-bit binary data into a 16-bit numpy array : temp = numpy.zeros((len(data) / 3, 4), dtype='b') temp[:, 1:] = numpy.frombuffer(data, dtype='b').reshape(-1, 3) temp2 = temp.view('> 16 # >> 16…
Basj
  • 41,386
  • 99
  • 383
  • 673
5
votes
3 answers

Using timeit module in a function with arguments

Example from documentation def test(): """Stupid test function""" L = [] for i in range(100): L.append(i) if __name__ == '__main__': import timeit print(timeit.timeit("test()", setup="from __main__ import test")) but…
Tom Cruise
  • 509
  • 1
  • 6
  • 10
5
votes
2 answers

Can we run multiple functions each with timeit in the same module

I would like to write multiple functions in the same Python module, each of which is a separate profiling test using timeit, so that I can use command line argument to specify which one to run. A naive example (profiling.py) would be: import…
MLister
  • 10,022
  • 18
  • 64
  • 92
4
votes
1 answer

How do I pass *args to my timeit.Timer object?

I originally made a custom function for timing functions that looks like this: def timefunc(function, *args): start = time.time() data = function(*args) end = time.time() time_taken = end - start print "Function:…
talloaktrees
  • 3,508
  • 6
  • 28
  • 43
4
votes
3 answers

Why is the first run of timeit usually slower than subsequent ones?

I've been testing some optimisations to a piece of code (specifically, whether elif n in [2,3] is faster than elif n == 2 or n == 3) and noticed something strange. Using timeit.default_timer I did several runs of each version of the function, and…
twigonometry
  • 166
  • 1
  • 9
4
votes
2 answers

python: bytecode-oriented profiler

I'm writing a web application (http://www.checkio.org/) which allows users to write python code. As one feedback metric among many, I'd like to enable profiling while running checks on this code. This is to allow users to get a very rough idea of…
bukzor
  • 37,539
  • 11
  • 77
  • 111
4
votes
1 answer

Why is [None] * 10 faster than [None for i in range(10)]

I wanted to create a list with some initialized values, because an empty list is not an option in python. So I started thinking which would be faster: l = [None for i in range(1000)] or l = [None] * 1000 And I tried testing it with with timeit: In…
evgeni tsvetanov
  • 173
  • 1
  • 13
4
votes
2 answers

Python - Overcome import in timeit

I recently noticed the following about the timeit python module: On my machine the lines: from timeit import Timer t = Timer(stmt='a = 2**3**4') print("This took {:.3f}s to execute.".format(t.timeit())) will produce: This took 0.017s to…
user9115052
  • 115
  • 1
  • 8
4
votes
1 answer

Why is lambda function execution time different when passed as callable vs being passed as a string statement to timeit.repeat()?

I got different results of the following two python timeit lines. print(min(timeit.repeat(lambda: 1+1))) print(min(timeit.repeat('lambda: 1+1'))) The output is something like: 0.13658121100002063 0.10372773000017332 Could you pls help explain the…
Penguin Bear
  • 67
  • 1
  • 2
4
votes
0 answers

Python timeit or custom Timer for production

We are currently using a custom Timer class (sample given below) to compute and log time taken for various pieces of modules in our Tornado app. I was exploring timeit module and wonder if we should make use of this instead. I understand that timeit…
Sha
  • 483
  • 4
  • 8
4
votes
2 answers

Experimentally determining computing complexity of matrix determinant

I need help determining experimentally the computing complexity of the determinant of a matrix nxn My code: import numpy as np import timeit t0 = time.time() for n in range(1, 10): A = np.random.rand(n, n) det =…
4
votes
1 answer

List concatenation using "+=" vs "extend" in Python 2 & Python 3

I was doing some analysis for doing list concatenation using two different approaches for varying lengths of lists, with different Python versions. Using += in Python 2: $ python -m timeit -s "li1=range(10**4); li2=range(10**4)" "li1 += li2" 10000…
kmario23
  • 57,311
  • 13
  • 161
  • 150
4
votes
0 answers

speed comparision between python native bitwise & numpy

I have some code and within this code I need to calculate a checksum (before transmission & on received data). Doing a simple timing check, a relatively large period of time was spent calculating and checking this. With 210,000 packets to deal with…
Naib
  • 999
  • 7
  • 20