How to use timeit module

后端 未结 14 2913
猫巷女王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 07:50

    I find the easiest way to use timeit is from the command line:

    Given test.py:

    def InsertionSort(): ...
    def TimSort(): ...
    

    run timeit like this:

    % python -mtimeit -s'import test' 'test.InsertionSort()'
    % python -mtimeit -s'import test' 'test.TimSort()'
    

提交回复
热议问题