登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

There's always a way

out. You just have to find it.

 
 
 

日志

 
 

".pyc"文件是什么文件  

2013-05-14 14:12:11|  分类: Python |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

这牵扯出来一个问题:Python是解释型(interpreted)语言还是编译型(compiled)语言?

shell我个人得出一个判断解释型和编译型的标准,就是写两条语句,一句对,一句错,看对的一句能不能执行:如果能执行,那就是解释型(逐行//块解释,后面的错不影响前面的执行);如果不能执行,那就是编译型(因为任何一句有错误,将导致编译不通过)。

 

>>> def func():

            print "hello"

            print (x,y)

>>> func()

hello

 

Traceback (most recent call last):

  File "<pyshell#17>", line 1, in <module>

    func()

  File "<pyshell#16>", line 3, in func

    print (x,y)

NameError: global name 'y' is not defined

 

光看输出好像是解释型的样子,但是正如下面介绍的,Python其实也包含了一个编译/转换成中间代码(bytecode)的过程。我自己网上查了资料,并没有一个定论,或许是二者兼而有之吧。这些仅仅摘抄一些观点:

 

Python’s high level data types, dynamic typing of objects and run-time invocation of the interpreter (using eval() or exec) together mean that a “compiled” Python program would probably consist mostly of calls into the Python run-time system, even for seemingly simple operations like x+1.

Python拥有高级数据类型,动态类型和解释器运行时调用,同时还伴随着一种“编译的”Python程序(可能由多数的对运行时系统的调用组成),即使是“x+1”这样的简单操作

 

Internally, Python source code is always translated into a bytecode representation, and this bytecode is then executed by the Python virtual machine. In order to avoid the overhead of repeatedly parsing and translating modules that rarely change, this byte code is written into a file whose name ends in ”.pyc” whenever a module is parsed. When the corresponding .py file is changed, it is parsed and translated again and the .pyc file is rewritten.

从内部来看,Python源代码总是被转换成bytecodebytecode之后将会被Python虚拟机执行。为了避免重复语法解析和转换那些极少发生改动的模块带来的overhead,当完成对一个module的语法解析之后,bytecode会被写入到一个“.pyc”的文件。而相应的py文件一旦发生改变(“.py”和“.pyc”是成对出现的,而且是“.pyc”依附于“.py”的关系,如果愿意,你也可以删除”.pyc”文件,这样并不会影响结果),对”.py”文件的再次语法解析和转换将会导致(与该”.py”文件)相应的“.pyc”文件被重写。

 

There is no performance difference once the .pyc file has been loaded, as the bytecode read from the .pyc file is exactly the same as the bytecode created by direct translation. The only difference is that loading code from a .pyc file is faster than parsing and translating a .py file, so the presence of precompiled .pyc files improves the start-up time of Python scripts. If desired, the Lib/compileall.py module can be used to create valid .pyc files for a given set of modules.

一旦”.pyc”文件被加载,那么,通过读取”.pyc”文件的bytecode来执行和通过转换成bytecode来执行再性能上并没有区别。唯一的差异只是从”.pyc”加载bytecode比直接解析转换一个“.py”文件要快。也就是说,通过加载预编译的“.pyc”文件能够改进执行Python脚本的启动/加载速度。(也就是说,执行bytecode是效率一样的,唯一的区别就是到bytecode准备好即将开始执行之前。”.pyc”保存的已经是bytecode,直接加载就可以执行;”.py”需要解析/转换成bytecode之后才可以执行。)

 

(Note that the main script executed by Python, even if its filename ends in .py, is not compiled to a .pyc file. It is compiled to bytecode, but the bytecode is not saved to a file. Usually main scripts are quite short, so this doesn’t cost much speed.)

但是也应该注意到,就算直接执行一个”.py”脚本,虽然没有编译成“.pyc”文件,但是它也会被编译成bytecode,只是bytecode没有存入文件(”.pyc”文件)中(存在内存中)。通常如果脚本很短的话,对执行速度并没有大的影响。

  评论这张
 
阅读(4990)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018