为什么python在解释之前将源码编译为字节码? [英] Why python compile the source to bytecode before interpreting?

查看:344
本文介绍了为什么python在解释之前将源码编译为字节码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么python在解释之前将源代码编译为字节码?

Why python compile the source to bytecode before interpreting?

为什么不直接从源代码解释?

Why not interpret from the source directly?

推荐答案

几乎没有解释器真的直接解释代码,一行一行 - 它太低效了。几乎所有的解释器使用一些可以容易执行的中间表示。此外,可以对此中间代码执行小的优化。

Nearly no interpreter really interprets code directly, line by line – it's simply too inefficient. Almost all interpreters use some intermediate representation which can be executed easily. Also, small optimizations can be performed on this intermediate code.

Python还存储此代码,这对下次执行此代码有巨大的优势:Python不必须解析代码;解析是编译过程中最慢的部分。因此,字节码表示大大减少了执行开销。

Python furthermore stores this code which has a huge advantage for the next time this code gets executed: Python doesn't have to parse the code anymore; parsing is the slowest part in the compile process. Thus, a bytecode representation reduces execution overhead quite substantially.

这篇关于为什么python在解释之前将源码编译为字节码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆