How to use timeit module

后端 未结 14 2635
猫巷女王i
猫巷女王i 2020-11-22 07:36

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

14条回答
  •  礼貌的吻别
    2020-11-22 08:03

    If you want to compare two blocks of code / functions quickly you could do:

    import timeit
    
    start_time = timeit.default_timer()
    func1()
    print(timeit.default_timer() - start_time)
    
    start_time = timeit.default_timer()
    func2()
    print(timeit.default_timer() - start_time)
    

提交回复
热议问题