How to measure the speed of a python function
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I usually write codes(functions) on www.codefights.com as a competitor.So speed is one of the important part of the code . How can i measure the speed of a certain code in python language whether it is the lambda function or a def function . 回答1: Have a look at the timeit module in pythons standard libaray: https://docs.python.org/2/library/timeit.html >>> import timeit >>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) 0.8187260627746582 >>> timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000) 0