简体   繁体   中英

Why does %timeit loop different number of times?

On Jupter Notebook, i was trying to compare time taken between the two methods for finding the index with max value.

在此处输入图像描述

In the Image, the first function took, 1000 loops, and the second took 10000 loops, is this increase in loops due to the method itself OR Jupyter Just added more loops to get more accurate time per loop even though the second function maybe took 1000 only, is that the case?

%timeit library will limit the number of runs depending on how long the script takes to execute.

The number of runs may be set with -n. Example:

%timeit -n 5000
df = pd.DataFrame({'High':[1,4,8,4,0]})

5000 loops, best of 3: 592 µs per loop

use -r to limit the number of run's:

import time
%timeit -r1 time.sleep(2)
# 2 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

%timeit -r4 time.sleep(2)
# 2 s ± 800 µs per loop (mean ± std. dev. of 4 runs, 1 loop each)

%timeit time.sleep(2)
# 2 s ± 46.5 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

It has a built-in option -n: " Options: -n: execute the given statement times in a loop. If this value is not given, a fitting value is chosen." docs

So it choses the number of loops itself if not specified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM