python代码优化技巧

吃西瓜放糖

来自: 吃西瓜放糖(the same old fears) 组长
2012-07-28 10:39:35

×
加入小组后即可参加投票
  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 11:38:59

    总结:在进行性能优化之前,要先使用性能分析工具(profile,cprofile,hotshot)对性能瓶颈做分析,找出代码中的热点再动手,对症下药。

  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 11:45:57

    上述代码中大量用到time模块进行代码运行时间的计时,补充一下: from time import time Functions: time() -- return current time in seconds since the Epoch as a float, i.e. 1343446821.615205 clock() -- return CPU time since process start as a float sleep() -- delay for a number of seconds given as a float, i.e. time.sleep(5)(这要能在现实中成真多好,最诱惑的方法) gmtime() -- convert seconds since Epoch to UTC tuple localtime() -- convert seconds since Epoch to local time tuple 上述两个方法都返回tuple,i.e. time.struct_time(tm_year=2012, tm_mon=7, tm_mday=28, tm_hour=3, tm_min=42, tm_sec=22, tm_wday=5, tm_yday=210, tm_isdst=0) asctime() -- convert time tuple to string ctime() -- convert time in seconds to string 上述两个方法都返回string表示的time,i.e. Sat Jul 28 11:43:15 2012 mktime() -- convert local time tuple to seconds since Epoch maketime? i.e. time.mktime(time.localtime()) strftime() -- convert time tuple to string according to format specification time.strftime, string format time, i.e. time.strftime('%Y%m%d') <==> datetime.now().strftime strptime() -- parse string to time tuple according to format specification <==> datetime.datetime.strptime tzset() -- change the local timezone

  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 11:48:46

    gmtime和localtime()如果单纯返回tuple,那取值只能用index: lt = time.localtime() print lt[0] 但想想写api的人不会这么笨,果然,lt.tm_year可用,可见是named tuple,但参数的名陈还是没有datetime.datetime好用

  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 17:22:06

    python性能优化工具: Psyco,Pypy,Cython,Pyrex psyco(http://psyco.sourceforge.net) In short: run your existing Python software much faster, with no change in your source Think of Psyco as a kind of just-in-time (JIT) compiler, a little bit like what exists for other languages, that emit machine code on the fly instead of interpreting your Python program step by step. The difference with the traditional approach to JIT compilers is that Psyco writes several version of the same blocks (a block is a bit of a function), which are optimized by being specialized to some kinds of variables (a "kind" can mean a type, but it is more general). The result is that your unmodified Python programs run faster. Benefits 2x to 100x speed-ups, typically 4x, with an unmodified Python interpreter and unmodified source code, just a dynamically loadable C extension module. Drawbacks Psyco currently uses a lot of memory. It only runs on Intel 386-compatible processors (under any OS) right now. There are some subtle semantic differences (i.e. bugs) with the way Python works; they should not be apparent in most programs. Psyco is unmaintained and dead. Please look at PyPy for the state-of-the-art in JIT compilers for Python. 总结:Psyco是个JIT编译器,目标是在不改动python源代码的情况下提高代码运行的性能,通常性能提升在4倍(范围是2~100倍)。它的缺点是比较耗内存,现在已经不再维护。

  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 17:24:07

    python性能优化工具: Psyco,Pypy,Cython,Pyrex psyco(http://psyco.sourceforge.net) python性能优化工具: Psyco,Pypy,Cython,Pyrex psyco(http://psyco.sourceforge.net) In short: run your existing Python software much faster, with no change in your source Think of Psyco as a kind of just-in-time (JIT) compiler, a little bit like what exists for other languages, that emit machine code on the fly instead of interpreting your Python program step by step. The difference with the traditional approach to JIT compilers is that Psyco writes several version of the same blocks (a block is a bit of a function), which are optimized by being specialized to some kinds of variables (a &quot;kind&quot; can mean a type, but it is more general). The result is that your unmodified Python programs run faster. Benefits 2x to 100x speed-ups, typically 4x, with an unmodified Python interpreter and unmodified source code, just a dynamically loadable C extension module. Drawbacks Psyco currently uses a lot of memory. It only runs on Intel 386-compatible processors (under any OS) right now. There are some subtle semantic differences (i.e. bugs) with the way Python works; they should not be apparent in most programs. Psyco is unmaintained and dead. Please look at PyPy for the state-of-the-art in JIT compilers for Python. 总结:Psyco是个JIT编译器,目标是在不改动python源代码的情况下提高代码运行的性能,通常性能提升在4倍(范围是2~100倍)。它的缺点是比较耗内存,现在已经不再维护。 ... 吃西瓜放糖

    这篇文章介绍psyco如何工作: http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-28/

  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 17:44:02

    PyPy(http://pypy.org/) PyPy is a fast, compliant alternative implementation of the Python language (2.7.2). It has several advantages and distinct features: Speed: thanks to its Just-in-Time compiler, Python programs often run faster on PyPy. (What is a JIT compiler?) Memory usage: large, memory-hungry Python programs might end up taking less space than they do in CPython. Compatibility: PyPy is highly compatible with existing python code. It supports ctypes and can run popular python libraries like twisted and django. Sandboxing: PyPy provides the ability to run untrusted code in a fully secure way. Stackless: PyPy comes by default with support for stackless mode, providing micro-threads for massive concurrency. 总结Pypy的特点: 1. 使用python实现了对python代码的hack。其实,只使用了python中一个子集rPython。可将python代码编译为C,CIL,Javascript 等目标代码。 2. 在pypy上运行的python代码速度会更快,比CPython快 3. 作为Psyco的替代者,Pypy做到了动态编译 4. 提供沙箱(运行速度会下降) 5. 提供微线程,stackless 6. 跟python代码兼容性很高

  • 吃西瓜放糖

    吃西瓜放糖 (the same old fears) 组长 楼主 2012-07-28 18:01:24

    Cython CPython is the default, most-widely used implementation of the Python programming language. It is written in C. In addition to CPython, there are two other production-quality Python implementations: Jython, written in Java, and IronPython, which is written for the Common Language Infrastructure. There are also several experimental implementations. CPython is a bytecode interpreter. It has a foreign function interface with several languages including C, in which one must explicitly write bindings in a language other than Python.

你的回应

回应请先 , 或 注册

6 人聚集在这个小组

最新讨论  ( 更多 )

↑回顶部