【原创】Python 之快速性能优化(第一部分)

简介:
本文为翻译,   原文地址:《   Quick Python Performance Optimization: Part I    

This is part I of a two part series of blog post on performance optimization in python.  
Aim is to just explain, the right way to do simple things which we use in day-to-day python programming, and has a very relevant performance impact.  
此文为关于 Python 性能优化的二连发博文的首篇 ,目标是解释清楚,如何按照正确且简单的方式做好每日 Python 编程,并且能获得好的性能体验。  

1. %timeit (per line) and %prun (cProfile) in ipython interactive shell.   
Profile your code while working on it, and try to find of where is the bottleneck. This is not contrary to the fact that premature optimization is the root of all evil. This is mean for the first level optimization and not a heavy optimization sequence.  
For more on profiling python code, you should read this: http://www.huyng.com/posts/python-performance-analysis/  
Another interesting library, line_profiler is for line by line profiling https://bitbucket.org/robertkern/line_profiler  
1. 在 ipython 交互式 shell 中使用    %timeit(每行使用)和       %prun(cProfile)  
在你开发代码的过程中时时进行性能测量,努力找出瓶颈所在。这种方式并不与“过早优化是万恶之源”的想法相违背。而是指第一个层次上的优化,而不是重度优化。  
关于对 python 代码的更多性能测量,可以参阅   这里    
另一个有意思的库 --    line_profiler   -- 可用于按行性能调优。  

2. Reduce the number of function calls. If you need a list to be manipluated, pass the entire list, rather than iterating over the list and passing  each element to the function and returning.  
2.  减少函数调用次数   。如果你需要对列表进行操作,那么请直接传入整个列表,而不是在列表上做迭代,然后分别将每一个列表元素传入函数再返回。  

3. Use xrange instead of range. (in Python2.x - this is by default in Python3.x)  
xrange is C implementation of range, with an eye on efficient memory usage.   
3.  使用 xrange 替代 range   。(在 Python2.x 中 - 在 Python3.x 中是默认行为)  
xrange 是 range 的 C 实现版本,主要增强了内存使用上的效率。  

4. For huge data, use  numpy   , its better than standard datastructures.  
4. 对于大块数据,请使用 numpy ,它比标准的数据结构要更优秀。  

5.  "".join(string)   is better than + or +=  
5. "".join(string) 比 + 或 += 更优秀。  

6.  while 1   is faster than while True  
6. while 1 比 while True 执行速度更快。  

7. list comphrension > for loop > while loop  
list comprehension is faster than looping over the list, and while loop is the slowest, with an external counter with it.  
7. 列表推导 > for 循环 > while 循环  
列表推导的运行速度快于在列表上做 for 循环,而在列表上做 while 循环是最慢的一种方式,因为其需要额外的计数器。  

8. use  cProfile   cStringIO   and  cPickle  
always use available C versions of the modules.  
8. 使用 cProfile、cStringIO 和 cPickle  
总是使用模块的 C 实现版本。  

9. Use  local variables   .  
local variables are faster than global variables, builtins or attribute lookups  
9. 使用本地变量。  
本地变量要快于全局变量、内置变量,或属性值的查找。  

10. list and iterators versions exist -  iterators   are memory efficient and scalable. Use  itertools  
Create generators and use yeild as much as posible. They are faster compared to the normal list way of doing it.  
http://www.diveinto.org/python3/iterators.html  
http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained  
Lets continue to part two for next level of quick optimization tricks here.  
10. 列表和迭代器 --  迭代器   具有内存方面的效率和可扩展特性。请使用 itertools 。  
创建生成器,并尽可能使用  yeild   。他们都比常规列表操作要更快。  
目录
相关文章
|
10月前
|
数据采集 存储 JSON
Python爬取知乎评论:多线程与异步爬虫的性能优化
Python爬取知乎评论:多线程与异步爬虫的性能优化
|
机器学习/深度学习 存储 设计模式
Python 高级编程与实战:深入理解性能优化与调试技巧
本文深入探讨了Python的性能优化与调试技巧,涵盖profiling、caching、Cython等优化工具,以及pdb、logging、assert等调试方法。通过实战项目,如优化斐波那契数列计算和调试Web应用,帮助读者掌握这些技术,提升编程效率。附有进一步学习资源,助力读者深入学习。
|
算法 测试技术 开发者
性能优化与代码审查:提升Python开发效率
【10月更文挑战第12天】本文探讨了Python开发中性能优化和代码审查的重要性,介绍了选择合适数据结构、使用生成器、避免全局变量等性能优化技巧,以及遵守编码规范、使用静态代码分析工具、编写单元测试等代码审查方法,旨在帮助开发者提升开发效率和代码质量。
207 5
|
算法 测试技术 开发者
性能优化与代码审查:提升Python开发效率
【10月更文挑战第6天】本文探讨了性能优化和代码审查在Python开发中的重要性,提供了选择合适数据结构、使用生成器、避免全局变量等性能优化技巧,以及遵守编码规范、使用静态代码分析工具、编写单元测试等代码审查方法,旨在帮助开发者提升开发效率和代码质量。
161 5
|
9月前
|
并行计算 算法 Java
Python3解释器深度解析与实战教程:从源码到性能优化的全路径探索
Python解释器不止CPython,还包括PyPy、MicroPython、GraalVM等,各具特色,适用于不同场景。本文深入解析Python解释器的工作原理、内存管理机制、GIL限制及其优化策略,并介绍性能调优工具链及未来发展方向,助力开发者提升Python应用性能。
538 0
|
10月前
|
数据采集 监控 算法
Python文件与目录比较全攻略:从基础操作到性能优化
文件比较的核心在于数据指纹校验,通过逐字节比对生成唯一标识,确保内容一致性。从标准库的os与filecmp到高性能第三方库如pydiffx,再到分布式与量子加密技术的未来趋势,文件比较广泛应用于数据备份、代码审查与系统监控等领域,是保障数据完整性的关键技术手段。
226 0
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
【8月更文挑战第5天】性能测试确保应用高负载下稳定运行。Apache JMeter与Locust是两大利器,助力识别解决性能瓶颈。本文介绍这两款工具的应用与优化技巧,并通过实战示例展示性能测试流程。首先,通过JMeter测试静态与动态资源;接着,利用Locust的Python脚本模拟HTTP请求。文中提供安装指南、命令行运行示例与性能优化建议,帮助读者掌握性能测试核心技能。
666 0
|
数据采集 搜索推荐 C语言
Python 高级编程与实战:深入理解性能优化与调试技巧
本文深入探讨了Python的性能优化和调试技巧,涵盖使用内置函数、列表推导式、生成器、`cProfile`、`numpy`等优化手段,以及`print`、`assert`、`pdb`和`logging`等调试方法。通过实战项目如优化排序算法和日志记录的Web爬虫,帮助你编写高效稳定的Python程序。
|
算法 测试技术 开发者
性能优化与代码审查:提升Python开发效率
探讨了Python开发中性能优化和代码审查的重要性,介绍了选择合适数据结构、使用生成器、避免全局变量等性能优化技巧,以及遵守编码规范、使用静态代码分析工具、编写单元测试等代码审查方法,旨在帮助开发者提升开发效率和代码质量。
156 8
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
650 1

推荐镜像

更多