user53670
user53670

Reputation:

Why python compile the source to bytecode before interpreting?

Why python compile the source to bytecode before interpreting?

Why not interpret from the source directly?

Upvotes: 17

Views: 5424

Answers (6)

John Leidegren
John Leidegren

Reputation: 60997

I doubt very much that the reason is performance, albeit be it a nice side effect. I would say that it's only natural to think a VM built around some high-level assembly language would be more practical than to find and replace text in some source code string.

Edit:

Okay, clearly, who ever put a -1 vote on my post without leaving a reasonable comment to explain knows very little about virtual machines (run-time environments).

http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Lars-Bak-Inside-V8-A-Javascript-Virtual-Machine/

Upvotes: 0

Paul Biggar
Paul Biggar

Reputation: 28739

Although there is a small efficiency aspect to it (you can store the bytecode on disk or in memory), its mostly engineering: it allows you separate parsing from interpreting. Parsers can often be nasty creatures, full of edge-cases and having to conform to esoteric rules like using just the right amount of lookahead and resolving shift-reduce problems. By contrast, interpreting is really simple: its just a big switch statement using the bytecode's opcode.

Upvotes: 3

Alex Martelli
Alex Martelli

Reputation: 881635

Re-lexing and parsing the source code over and over, rather than doing it just once (most often on the first import), would obviously be a silly and pointless waste of effort.

Upvotes: 6

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

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 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.

Upvotes: 39

David Webb
David Webb

Reputation: 193706

Because you can compile to a .pyc once and interpret from it many times.

So if you're running a script many times you only have the overhead of parsing the source code once.

Upvotes: 8

Brian
Brian

Reputation: 25834

Because interpretting from bytecode directly is faster. It avoids the need to do lexing, for one thing.

Upvotes: 7

Related Questions