82

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

Ruby is no doubt an interpreted language since the source code is processed 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 create 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?

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
crodjer
  • 1,039
  • 1
  • 9
  • 10
  • 1
    When is there doubt as to whether Ruby is an interpreted language? When it's compiled. :) http://www.macruby.org/ – mipadi Dec 08 '10 at 14:34
  • 8
    It is worth noting that no modern language is interpreted in the strict sense. Virtually all of them compile to bytecode. – Winston Ewert Dec 08 '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 08 '10 at 18:13
  • @S.Lott, I have run into some which appear to be actually interpreted. These are typically internal scripting languages of applications. – Winston Ewert Dec 08 '10 at 20:45
  • @Winston Ewert: That's okay. Your original point that almost everything is byte-code compiled was spot on. The presence of "internal scripting languages of applications" that somehow bypass byte-code compilation isn't really relevant. The question is about Python and other very-widely used languages. – S.Lott Dec 08 '10 at 20:50
  • 6
    >> On the contrary C is a compiled language << http://root.cern.ch/drupal/content/cint – igouy Dec 11 '10 at 18:34
  • 3
    @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 08 '13 at 22:30
  • precompiled raw machine code is not by default faster than a good interpreter – Thorbjørn Ravn Andersen Oct 23 '15 at 08:35
  • "On the contrary C is a compiled language, as one have to compile the source code first according to the machine and then execute.": Nope, I once used a C interpreter to do some experiments. – Giorgio Nov 05 '15 at 06:03
  • see also: [Why does Python need both a compiler and an interpreter?](http://programmers.stackexchange.com/a/289468) – gnat Jan 12 '16 at 15:57

5 Answers5

87

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

mipadi
  • 7,493
  • 36
  • 35
  • 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 08 '10 at 15:02
  • I think you're actually describing that Python is either what I would call 'semi-compiled', or can actually be full compiled. By semi-compiled I mean that since it is usually compiled to the 'intermediate language' .pyc file that is used by the Python Virtual Machine, it is usually being run from this 'semi-compiled' form, that generally makes code faster than plain run-time interpretation of interpreted code. Interestingly, semi-compiled code can sometimes be faster than natively compiled code (e.g. C# is generally faster than C++). – Chris Halcrow Jun 25 '15 at 04:16
  • 6
    Cython compiles Python code to C so that it can be compiled as a shared object. – greyfade Oct 23 '15 at 06:04
  • Distinguishing byte code and machine code in this fashion is pretty arbitrary. Java is compiled: the javac compiler produces class files containing low level instructions that may be executed, either in a virtual machine (eg hotspot) or directly by hardware (e.g. on ARM processors with the Jazelle extension). As far as I know, there is no technical reason a similar processor architecture couldn't be designed to directly execute python vm instructions. – Jules Oct 23 '15 at 12:11
  • @Jules Coincidentally, [Jython](http://www.jython.org/) code is actually compiled in to .class files which I believe are reused until you modify the py source. – JimmyJames Aug 18 '17 at 16:41
  • @Jules, there are several Java hardware machines, e.g. ARM [Jazelle](https://en.wikipedia.org/wiki/Jazelle) – Paul Draper Aug 18 '17 at 17:55
  • thx !! Just got bit confused with two statements _Some Python implementations, however, do consist of a just-in-time compiler that will compile Python byte code into native machine code_ **AND** _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_ I assume you referring to same thing - Compilers to convert python to python byte code and then python virtual machine to convert from byte code to native code. Isn't it ? – rahulaga-msft Nov 01 '18 at 12:22
36

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.

Maroun
  • 103
  • 4
aufather
  • 4,449
  • 2
  • 26
  • 24
  • thanx for the info.According to the benchmarks the performance of python is way down! – crodjer Dec 08 '10 at 08: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). – Eric O. Lebigot Dec 08 '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. – Eric O. Lebigot Dec 08 '10 at 10:24
  • @EOL you are correct the simple and easy to understand syntax of python make make programming with it much speedier. Ans as far as execution speed comparisons are concerned, I found out many other benchmarks here at stack-exchange and otherwise which suggest that the benchmarks cannot be perfect and depend on context and the task which is being performed. – crodjer Dec 08 '10 at 10:44
  • 1
    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 08 '10 at 14:42
  • There is Psycho compiler for python which makes python codes run faster than C counter parts. Its really an interesting research. If it succeeds then Python would be the choice for a lot of programming (even for low level stuffs). –  Dec 09 '10 at 04:42
  • 1
    @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." http://psyco.sourceforge.net/introduction.html – igouy Dec 11 '10 at 18:25
  • 1
    @jase21 - "makes python codes run faster than C counter parts" - Are we supposed to just take your word for that? – igouy Dec 11 '10 at 18:28
  • igouy: http://mail.python.org/pipermail/python-dev/2004-April/044002.html –  Dec 13 '10 at 04:11
  • @jase21 - that post seems to be talking about psyco codes being a little faster than Python codes which wrap C codes, not about psyco codes compared to C codes. – igouy Jan 14 '11 at 15:00
  • @aufather >> The link I gave very clearly states these are flawed benchmarks... << No, what it very clearly stated was that benchmarks *can* be flawed, and people sometimes over generalize from particular measurements. I've rewritten it to try and make the points clearer. – igouy Jan 14 '11 at 15:07
  • 3
    Link in the answer is broken. – Basilevs Jan 02 '16 at 05:09
12

Compiled vs. interpreted can be helpful in some contexts, but when applied in a technical sense, it is a false dichotomy.

A compiler (in the broadest sense) is a translator. It translates program A to program B and for future execution it using a machine M.

An interpreter (in the broadest sense) is an executor. It is a machine M that executes program A. Though we typically exclude from this definitions physical machines (or non-physical machines that act just like physical ones). But from theoretic perspective, that distinction is somewhat arbitrary.


For example, take re.compile. It "compiles" a regex to an intermediate form, and that intermediate form is interpreted/evaluated/executed.


In the end, it depends on what level abstraction you are talking about, and what you care about. People say "compiled" or "interpreted" as broad descriptions of the most interesting parts of the process, but really most every program is compiled (translated) and interpreted (executed) in one way or another.

CPython (the most popular implementation of the Python language) is mostly interesting for executing code. So CPython would typically be described as interpreted. Though this is a loose label.

Paul Draper
  • 5,972
  • 3
  • 22
  • 37
7

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.

LennyProgrammers
  • 5,649
  • 24
  • 37
5

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.

ykombinator
  • 4,317
  • 4
  • 34
  • 52