[Fwd: Re: [Discuss] Interpreters vs Compilers]
David Bronaugh
dbronaugh at linuxboxen.org
Thu Mar 13 15:56:30 PDT 2008
John Blomfield wrote:
> pw wrote:
>> John Blomfield wrote:
>>>
>>> I don't have anything more authoritative to add than I did last
>>> night, except to say again, that Python and Java for example compile
>>> their source code into "bytecode" which is slightly optimized code
>>> but it is not machine code. A byte code file still has to be
>>> interpreted, which means translated into machine code instructions
>>> line by line by the so called "Virtual Machine". Each platform
>>> needs the Virtual Machine running to run a bytecode file. In the
>>> case of a loop, the loop is in bytecode and must be interpreted line
>>> by line every time through the loop, into possibly many many machine
>>> instructions. In the case of a compiled C program the binary is in
>>> machine instructions that the CPU understands without
>>> interpretation, hence the speed. The optimization in C is very very
>>> good, so good that it is almost impossible for the average
>>> programmer to do better using Assembler. I once did some
>>> programming of micro-controllers, which are quite slow compared to
>>> the typical PC, in both Microchip Assembler and Microchip C and the
>>> C compiler always won. This one of the reasons most hardware is
>>> programmed in C, including Linux and Python I think. Python
>>> certainly uses a lot of built in mathematical functions that are
>>> written in C to help speed its execution.
>>>
>>> Its all a question of horses for courses! Scripting languages like
>>> Perl and Python are often referred to as the "glue" that sticks the
>>> C and C++ modules together. So if you want Zoom Zoom go C!
>>>
>>> John Blomfield
>>
>> byte code is a type of pseudo machine code.
>> This code is run in a virtual machine much like
>> emulating the operation of a microprocessor.
>>
>> The java virtual machine, for example, is merely a
>> microprocessor/toolkit architecture that has been defined
>> to use a machine language of java byte code. The virtual machine
>> is compiled for various computers to provide java system emulation.
>>
>> The speed of this emulation is related to the optimizations built
>> into the virtual machine as compiled for your particular system.
>>
>> (ie: system meaning I86 or PPC or sparc or etc..)
>>
>> Hence, the byte code process is not exactly akin to
>> 'interpretation'. The interpretation has already been
>> done in the compilation from source code to byte code.
>>
>> In theory byte code processing can be as fast as
>> machine code since the virtual machine can directly
>> transfer the values of byte code to applicable machine
>> level instructions prior to running the application,
>> thus running the application in 'native' machine code.
>>
> Are you saying that the Python Virtual Machine reads all the pyc
> bytecode file and converts it to binary and then runs it? If so this
> is contrary to my understanding. About Java I am not sure.
The Python interpreter compiles Python code to bytecode, and then the VM
runs it.
FWIW, the Java VM (1.3, at least) is about 4x slower than C/C++ when I
tested it. The Ocaml bytecode interpreter is about 18x slower. I haven't
done any playing around with Python bytecode, but I would assume it'd
fall somewhere in between those two speeds.
The only really fair way to test how much better a bytecode interpreter
is, is to test it on a short bit of code that takes very little time to
convert to bytecode, but takes a proportionally much longer time to run.
Then you see the full advantages without confounding effects of the time
required to convert to bytecode.
It's true what pw says; at least in theory, it should be possible for
bytecode to run as fast as native. But in practice it doesn't tend to
work out that way.
David
More information about the Discuss
mailing list