Why does %timeit loop different number of times?

后端 未结 3 1769
你的背包
你的背包 2021-02-18 22:12

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

相关标签:
3条回答
  • 2021-02-18 22:23

    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.

    0 讨论(0)
  • 2021-02-18 22:29

    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)
    
    0 讨论(0)
  • 2021-02-18 22:36

    %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
    
    0 讨论(0)
提交回复
热议问题