Python 代码优化常见技巧代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构、优化、扩展以及文档相关的事情通常需要消耗 80% 的工作量。优化通常包含两方面的内容:减小代码的体积,提高代码的运行效率。改进算法,选择合适的数据结构一个良好的算法能够对性能起到关键作用,因此性能改进的首要点是对算法的改进。在算法的时间复杂度排序上依次是:O(1) -> O(lg n) -> O(n lg n) -> O(n^2) -> O(n^3) -> O(n^k) -> O(k^n) -> O(n!)因此如果能够在时间复杂度上对算法进行一定的改进,对性能的提高不言而喻。但对具体算法的改进不属于本文讨论的范围,读者
Update: There is a part #2 of the benchmark results. This page has been translated intoSpanish by Maria Ramos.This all began as a colleague of mine stated that Python was so damn slow for maths. Which really astonished me and made me check it out, as my father told me once that he was very satisfied with Python, as it was very maths oriented.T
两个这样的测试页面:1、http://maettig.com/code/php/php-performance-benchmarks.php2、http://www.phpbench.com/Check if a String is emptyMethodUndefinedNullFalseEmpty stringString "0"String "1"Long stringSummaryIndexif (!$var)2 ms>0 ms>0 ms>0 ms>0 ms>0 ms>0 ms4 ms132if (empty($var))>0 ms1