1-Python - 如何提升Python代码的性能

before

Python的性能一直是人们诟病的地方,所以,我们在日常的搬砖中,要注意使用技巧提高搬砖效率,也能加深你对Python的理解。

使用 if is

In [3]: %timeit -n 1000 [i for i in range(100000) if i == True]
3.38 ms ± 41.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [4]: %timeit -n 1000 [i for i in range(100000) if i is True]
2.6 ms ± 47.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

that's all,see also:

提升Python程序运行效率的方法总结

posted @ 2020-11-11 10:51  听雨危楼  阅读(262)  评论(0编辑  收藏  举报