How do I get time of a Python program's execution?

后端 未结 30 2102
甜味超标
甜味超标 2020-11-22 02:20

I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.

I\'ve looked at the timeit

30条回答
  •  迷失自我
    2020-11-22 03:06

    Later answer, but I use timeit:

    import timeit
    code_to_test = """
    a = range(100000)
    b = []
    for i in a:
        b.append(i*2)
    """
    elapsed_time = timeit.timeit(code_to_test, number=500)
    print(elapsed_time)
    # 10.159821493085474
    

    • Wrap all your code, including any imports you may have, inside code_to_test.
    • number argument specifies the amount of times the code should repeat.
    • Demo

提交回复
热议问题