Take the 2-minute tour ×

This is just a wondering I had while reading about interpreted and compiled languages.

Ruby is no doubt an interpreted language, since source code is compiled by an interpreter at the point of execution.
On the contrary C is a compiled language, as one have to compile the source code first according to the machine and then execute. This results is much faster execution.

Now coming to Python:

  • A python code (somefile.py) when imported creates a file (somefile.pyc) in the same directory. Let us say the import is done in a python shell or django module. After the import I change the code a bit and execute the imported functions again to find that it is still running the old code. This suggests that *.pyc files are compiled python files similar to executable created after compilation of a C file, though I can't execute *.pyc file directly.
  • When the python file (somefile.py) is executed directly ( ./somefile.py or python somefile.py ) no .pyc file is created and the code is executed as is indicating interpreted behavior.

These suggest that a python code is compiled every time it is imported in a new process to crate a .pyc while it is interpreted when directly executed.

So which type of language should I consider it as? Interpreted or Compiled? And how does its efficiency compare to interpreted and compiled languages?

According to wiki's Interpreted Languages page it is listed as a language compiled to Virtual Machine Code, what is meant by that?


Looking at the answers it seems that there cannot be a perfect answer to my questions. Languages are not only interpreted or only compiled, but there is a spectrum of possibilities between interpreting and compiling.

Also, its not the languages which are interpreted or compiled, but rather their implementations either interpret or compile code.
I also found out about Just in time compilation

From the answers by aufather, mipadi, Lenny222, ykombinator, comments and wiki I found out that in python's major implementations it is compiled to bytecode, which is a highly compressed and optimized representation and is machine code for a virtual machine, which is implemented not in hardware, but in the bytecode interpreter.

As far as execution speed is concerned the various benchmarks cannot be perfect and depend on context and the task which is being performed.

Please let me know if I am wrong in my interpretations.

share|improve this question
When is there doubt as to whether Ruby is an interpreted language? When it's compiled. :) macruby.org –  mipadi Dec 8 '10 at 14:34
It is worth noting that no modern language is interpreted in the strict sense. Virtually all of them compile to bytecode. –  Winston Ewert Dec 8 '10 at 14:59
@Winston Ewert: bravo! Applesoft Basic (in 1980's) was byte-code compiled. "modern" in this case, means every interpreted language in living memory with the only possible exception being some rudimentary Dartmouth Basic implementations. –  S.Lott Dec 8 '10 at 18:13
>> On the contrary C is a compiled language << root.cern.ch/drupal/content/cint –  igouy Dec 11 '10 at 18:34
@S.Lott: Calling the tokenization process that Applesoft and '80s BASIC interpreters did "bytecode compilation" is more than a little disingenuous. Yes, the program code entered by the user was stored in memory in a compressed form, one byte per reserved word, but nothing was done beyond that until you typed RUN. It was as if you had a compiler that did the lexing step and then output a stream of tokens that had to be reparsed every time the program was run. Not at all like modern bytecode compilation as done by, say, javac, which encompasses lexing, parsing, and optimization. –  dodgethesteamroller Jul 8 '13 at 22:30

4 Answers 4

up vote 28 down vote accepted

It's worth noting that languages are not interpreted or compiled, but rather language implementations either interpret or compile code. You noted that Ruby is an "interpreted language", but you can compile Ruby à la MacRuby, so it's not always an interpreted language.

Pretty much every Python implementation consists of an interpreter (rather than a compiler). The .pyc files you see are byte code for the Python virtual machine (similar to Java's .class files). They are not the same as the machine code generated by a C compiler for a native machine architecture. Some Python implementations, however, do consist of a just-in-time compiler that will compile Python byte code into native machine code.

(I say "pretty much every" because I don't know of any native machine compilers for Python, but I don't want to claim that none exist anywhere.)

share|improve this answer
Depending on your definition, native machine compilers for Python exist. Some only compile a subset of python. Others implement all of python but use the python API to actually perform the operations which it cannot perform in C. –  Winston Ewert Dec 8 '10 at 15:02

Python will fall under byte code interpreted. .py source code is first compiled to byte code as .pyc. This byte code can be interpreted (official CPython), or JIT compiled (PyPy). Python source code (.py) can be compiled to different byte code also like IronPython (.Net) or Jython (JVM). There are multiple implementations of Python language. The official one is a byte code interpreted one. There are byte code JIT compiled implementations too.

For speed comparisons of various implementations of languages you can try here.

share|improve this answer
thanx for the info.According to the benchmarks the performance of python is way down! –  crodjer Dec 8 '10 at 8:18
@dcrodjer: The philosophy of Python, and of scripting languages in general, is that the programmer's performance is way up: Python is considered 2 to 10 times faster than C, in terms of the time it takes to write and maintain code (this is directly correlated to the fewer lines of code, as seen in aufather's link). Furthermore, Python has many tools that allows it to sometimes reach near C-speed (Cython, PyPy, or specialized modules like NumPy or SciPy). –  EOL Dec 8 '10 at 10:04
@aufather: The speed comparison that aufather points to do not use what people would concretely used for the task at hand: they would use the fast, well-supported, and convenient mathematical libraries NumPy and SciPy, for many of the examples. Thus, Python is is practice sometimes much faster and simpler than what the benchmarks show. –  EOL Dec 8 '10 at 10:24
The link I gave very clearly states these are flawed benchmarks of language implementations. Python should not be your choice of language if you worry too much about execution performance. If you still want to compare, compare similar languages. Byte code interpreted official CPython is comparable to or faster than JIT compiled Ruby. –  aufather Dec 8 '10 at 14:42
@jase21 - "My plans for 2006 are to port the techniques implemented in Psyco to PyPy. PyPy will allow us to build a more flexible JIT specializer, easier to experiment with, and without the overhead of having to keep in sync with the evolutions of the Python language." psyco.sourceforge.net/introduction.html –  igouy Dec 11 '10 at 18:25

Virtual Machine Code is a more compact version of the original source code (byte code). It still needs to be interpreted by a virtual machine, since it is no machine code. It's easier and faster to parse than the original code written by a human being, though.

Some virtual machines generate machine code while interpreting the virtual machine code for the first time (just in time compilation - JIT). The following invocations will use this machine code directly, which leads to faster execution.

As far as i know Ruby >= 1.9 uses a virtual machine like Python too.

share|improve this answer

The Python runtime runs custom object code(byte code) on a virtual machine.

The compilation process converts source code to object code.

To speed things up, the object code (or byte code, if you prefer) is stored on disk, so it can be reused the next time the program is run.

share|improve this answer

protected by gnat Jan 27 at 14:41


Would you like to answer one of these unanswered questions instead?
