Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

timeit.repeat

Collects separate elapsed durations from repeated timing experiments.

Version: Python 3.14; repository image, production runtime not attested

Syntax

timeit.repeat(stmt='pass', setup='pass', timer=timeit.default_timer, repeat=5, number=1000000, globals=None)

Collects separate elapsed durations from repeated timing experiments.

Each returned duration covers the full repetition count of one experiment. Assertions about the number of samples are deterministic while timing values vary.

Run one setup and number timed operations on each of repeat trials. The returned list is in trial order and contains totals, not per-operation averages. The lowest duration usually approximates the least interrupted run. With repeat=3 and number=2 the example returns three durations and calls the function six times.

import timeit
ticks=iter([0.,2.,5.,8.,10.,11.])
events=[]
times=timeit.repeat(lambda:events.append('call'),lambda:events.append('setup'),timer=lambda:next(ticks),repeat=3,number=2)
assert times==[2.,3.,1.] and events==['setup','call','call']*3
assert timeit.repeat('pass',repeat=0)==[]

Examples

import timeit
calls = []
times = timeit.repeat(lambda: calls.append(1), repeat=3, number=2)
print(len(times), len(calls), all(t >= 0 for t in times))

Result

3 6 True

Run one setup and number timed operations on each of repeat trials. The returned list is in trial order and contains totals, not per-operation averages. The lowest duration usually approximates the least interrupted run. With repeat=3 and number=2 the example returns three durations and calls the function six times.

Sources