对于计时有两个十分有用的魔法命令:%%time 和 %timeit。
%time [x for x in range(1000)]
Wall time: 0 ns
Out[22]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
%%timeit
li = []
for i in range(1000):
li.append(i ** 2)
321 µs ± 2.03 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%time
li = []
for i in range(1000):
li.append(i ** 2)
Wall time: 0 ns