如果你想知道一小段Python代码执行效率如何,可以试试timeit模块。这个模块提供了一种简单的方法来对一小段Python代码进行计时。它同时具有命令行接口和可调用接口。它避免了许多测量执行时间的常见陷阱。
# The "timeit" module lets you measure the execution
# time of small bits of Python code
>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))',
number=10000)
0.3412662749997253
>>> timeit.timeit('"-".join([str(n) for n in range(100)])',
number=10000)
0.2996307989997149
>>> timeit.timeit('"-".join(map(str, range(100)))',
number=10000)
0.24581470699922647
详细介绍:http://docs.python.org/3/library/timeit.html#command-line-interface