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
2019
votes
41 answers

How do I measure elapsed time in Python?

I want to measure the time it took to execute a function. I couldn't get timeit to work: import timeit start = timeit.timeit() print("hello") end = timeit.timeit() print(end - start)
gilbert8
  • 20,223
  • 3
  • 14
  • 3
462
votes
15 answers

How to use timeit module

How do I use timeit to compare the performance of my own functions such as "insertion_sort" and "tim_sort"?
Neemaximo
  • 20,031
  • 12
  • 32
  • 40
255
votes
5 answers

Creating an empty list in Python

What is the best way to create a new empty list in Python? l = [] or l = list() I am asking this because of two reasons: Technical reasons, as to which is faster. (creating a class causes overhead?) Code readability - which one is the standard…
user225312
  • 126,773
  • 69
  • 172
  • 181
239
votes
9 answers

How can I time a code segment for testing performance with Pythons timeit?

I've a python script which works just as it should, but I need to write the execution time. I've googled that I should use timeit but I can't seem to get it to work. My Python script looks like this: import sys import getopt import timeit import…
Emil Devantie Brockdorff
  • 4,724
  • 12
  • 59
  • 76
152
votes
5 answers

What is %timeit in Python?

I always read the code to calculate the time like this way: %timeit function() What does "%" mean here? I think, the "%" is always used to replace something in a string, like %s means replace a string, %d replace a data, but I have no idea about…
xirururu
  • 5,028
  • 9
  • 35
  • 64
133
votes
3 answers

Why is it slower to iterate over a small string than a small list?

I was playing around with timeit and noticed that doing a simple list comprehension over a small string took longer than doing the same operation on a list of small single character strings. Any explanation? It's almost 1.35 times as much time. >>>…
Sunjay Varma
  • 5,007
  • 6
  • 34
  • 51
114
votes
9 answers

timeit versus timing decorator

I'm trying to time some code. First I used a timing decorator: #!/usr/bin/env python import time from itertools import izip from random import shuffle def timing_val(func): def wrapper(*arg, **kw): '''source:…
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
105
votes
5 answers

Getting "global name 'foo' is not defined" with Python's timeit

I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called timeit that purports to do exactly that: import timeit def foo(): # ... contains code I…
Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164
57
votes
3 answers

Measure website load time with Python requests

I'm trying to build a tool for testing the delay of my internet connection, more specifically web site load times. I thought of using the python requests module for the loading part. Problem is, it's got no built-in functionality to measure the time…
cookM
  • 953
  • 3
  • 8
  • 11
52
votes
1 answer

timeit and its default_timer completely disagree

I benchmarked these two functions (they unzip pairs back into source lists, came from here): n = 10**7 a = list(range(n)) b = list(range(n)) pairs = list(zip(a, b)) def f1(a, b, pairs): a[:], b[:] = zip(*pairs) def f2(a, b, pairs): for i,…
superb rain
  • 5,300
  • 2
  • 11
  • 25
51
votes
7 answers

accurately measure time python function takes

I need to measure the time certain parts of my program take (not for debugging but as a feature in the output). Accuracy is important because the total time will be a fraction of a second. I was going to use the time module when I came across…
hoju
  • 28,392
  • 37
  • 134
  • 178
47
votes
1 answer

What unit of time does timeit return?

I don't know how to interpret the output from Python's timeit.timeit() function. My code is as follows: import timeit setup = """ import pydash list_of_objs = [ {}, {'a': 1, 'b': 2, 0: 0}, {'a': 1, 'c': 1, 'p': lambda x:…
nivix zixer
  • 1,611
  • 1
  • 13
  • 19
46
votes
5 answers

When should I ever use file.read() or file.readlines()?

I noticed that if I iterate over a file that I opened, it is much faster to iterate over it without reading it first. That is: l = open('file','r') for line in l: ... is much faster than l = open('file','r') for line in l.read(): ... or l…
Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66
39
votes
9 answers

How can I capture return value with Python timeit module?

Im running several machine learning algorithms with sklearn in a for loop and want to see how long each of them takes. The problem is I also need to return a value and DONT want to have to run it more than once because each algorithm takes so long.…
Leon
  • 5,701
  • 3
  • 38
  • 38
38
votes
2 answers

Why is if True slower than if 1?

Why is if True slower than if 1 in Python? Shouldn't if True be faster than if 1? I was trying to learn the timeit module. Starting with the basics, I tried these: >>> def test1(): ... if True: ... return 1 ... else: ... …
thiruvenkadam
  • 4,170
  • 4
  • 27
  • 26
1
2 3
26 27