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".

341 questions
1519
votes
38 answers

How to measure elapsed time in Python?

What I want is to start counting time somewhere in my code and then get the passed time, to measure the time it took to execute few function. I think I'm using the timeit module wrong, but the docs are just confusing for me. import timeit start =…
gilbert8
  • 15,207
  • 3
  • 12
  • 3
395
votes
14 answers

How to use timeit module

I understand the concept of what timeit does but I am not sure how to implement it in my code. How can I compare two functions, say insertion_sort and tim_sort, with timeit?
Neemaximo
  • 16,509
  • 10
  • 27
  • 38
250
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
  • 108,033
  • 64
  • 161
  • 179
192
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,086
  • 11
  • 55
  • 76
132
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
  • 4,435
  • 6
  • 32
  • 48
94
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
  • 72,761
  • 40
  • 144
  • 160
72
votes
8 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
  • 711,858
  • 148
  • 1,594
  • 1,547
48
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
  • 833
  • 3
  • 8
  • 11
44
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
  • 24,959
  • 33
  • 122
  • 169
36
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,461
  • 1
  • 11
  • 19
36
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
  • 217,951
  • 48
  • 415
  • 461
35
votes
8 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
  • 4,365
  • 3
  • 33
  • 34
34
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
  • 3,623
  • 1
  • 23
  • 25
33
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 "read"-ing it. i.e. l = open('file','r') for line in l: pass (or code) is much faster than l = open('file','r') for line in l.read() /…
Maverick Meerkat
  • 3,847
  • 2
  • 31
  • 49
28
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
  • 665
  • 8
  • 12
1
2 3
22 23