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
-1
votes
1 answer

python-2.7 timeit function crashes

For some reason, the Python-2.7 timeit function crashes in the following example: a,b = 0,0 timeit a=b # ok: 10000000 loops, best of 3: 50.9 ns per loop timeit if a==a+b: pass # ok: 1000000 loops, best of 3: 129 ns per…
Rolf Bartstra
  • 1,643
  • 1
  • 16
  • 19
-2
votes
1 answer

Time units returned from timeit

At the first glance, Python documentation at https://docs.python.org/3.10/library/timeit.html# suggests that the time units returned by timeit with default parameters are the ones of the default timer time.perf_counter(), which are fractional…
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
-2
votes
3 answers

unexpected indent for python timeit

I have a problem with doing timeit for python. Here is codes import timeit >>> iteration_test = """ for i in itr : pass """ >>> >>> timeit.timeit(iteration_test, setup='itr = list(range(10000))', number=1000) I tried tab…
-2
votes
1 answer

Accessing the workshet with openpyxl. Which methot is faster?

I am refactoring a small project which bases on openpyxl to analyze .xlsx inputs. I am trying to reduce time needed for it to do its job and currently trying methods for accessing cell values of Excel worksheet. I tested two methods (code below)…
w8eight
  • 605
  • 1
  • 6
  • 21
-2
votes
1 answer

what is the unit of timeit() from python3 timeit module function

import time import timeit start = timeit.timeit() time.sleep(5) end = timeit.timeit() time_elapsed = end - start print(end) print(time_elapsed) print(start) Outputs: 0.018255482330552297 -0.00033663523232263515 0.018592117562874932
tuck yew
  • 11
  • 2
-2
votes
1 answer

numpy array using more loops than it should. why?

%timeit [i **2 for i in range(1000)] print('*******') %timeit np.arange(1000) ** 2 prints: 1000 loops, best of 3: 376 µs per loop ******* The slowest run took 11.43 times longer than the fastest. This could mean that an intermediate result is…
claudius
  • 1,112
  • 1
  • 10
  • 23
-3
votes
2 answers

Understanding the results of Python's timeit module

I'm new to using Python's timeit module to benchmark code, but the results I'm getting make me think that I'm misunderstanding how to interpret the results. This question has two parts: Part A. In the code below, I'm using timeit to measure the…
Rebitzele
  • 3,252
  • 1
  • 20
  • 21
-3
votes
3 answers

Does python timeit consider setup in the count

I'm using python timeit to see how long it takes for a function to run. setup = '''from __main__ import my_func import random l = list(range(10000)) random.shuffle(l)''' timeit.timeit('my_func(l)', setup=setup, number=1000) the…
Lian
  • 3
  • 1
-4
votes
1 answer

why does timeit module take longer time than the code produces output quickly in python?

here is the code to calculate fibonacci import timeit counter=0 def fibhelper(n): global counter counter+=1 if n==0 : return 0 elif n==1: return 1 else: return fibhelper(n-1)+fibhelper(n-2) print fibhelper(20) print "Total…
brain storm
  • 30,124
  • 69
  • 225
  • 393
-6
votes
2 answers

quotation marks needed for variable name in timeit moduel, python3

import random import timeit data = [random.randint(-10,10) for i in range(10)] timeit.timeit('filter(lambda x : x>=0 ,"data")') As shown in the code. if I try to remove the "" from "data", it would throw an error. Why? There is still a single…
1 2 3
26
27