".pyc"文件是什么文件

转自:http://lishicongli.blog.163.com/blog/static/14682590201341421211719/

这牵扯出来一个问题: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源代码总是被转换成bytecode,bytecode之后将会被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”文件)中(存在内存中)。通常如果脚本很短的话,对执行速度并没有大的影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值