2

I am an (old) engineer not a programmer so forgive me for asking a naïve question.

My understanding is that to get really fast execution times for a program, it needs to be compiled to native machine code. And there are a relatively small number of languages still in use that do this (e.g. C and C++).

But I much prefer the syntax of Python over that of the C-derived compiled languages. However my understanding is that interpreted Python (and pseudo-compiled Python run on a virtual machine) cannot match the execution speed of a truly compiled language.

Is there some reason that a true native-code Python compiler cannot be developed?

[I am interested specifically in Python but I am not aware of any language that can be interpreted and also compiled to native machine code.]

  • What about Cython? Does that not count? Or perhaps Nim? – UnholySheep Sep 05 '21 at 18:34
  • 4
    Whether a language is compiled or interpreted is an implementation decision. You could write both a compiler and an interpreter for a given language, it just isn't common. – Bill the Lizard Sep 05 '21 at 18:34
  • Covered already here in the answers? (they do mention true compilers, also try a search n "python jit compiler" for similar) - https://stackoverflow.com/questions/13034991/does-the-python-3-interpreter-have-a-jit-feature – Dave S Sep 05 '21 at 18:42
  • 2
    The execution speed that you like in other languages comes from optimizations that the compiler is able to do. These optimizations are difficult in dynamically typed languages because a lot of the things you may want to know in order to make the optimizations aren't known until runtime when it's too late. – Mark Sep 05 '21 at 18:55

1 Answers1

3

The key distinction is a clean separation between compile time and run time. In Python, for instance, import happens at runtime, and can happen conditionally. And per the halting problem, that means a compiler cannot determine up front if a given import will happen. Yet, this affects the code that would need to be generated.

As Bill the Lizard notes, if the language does have a clean distinction, an interpreter can still choose to ignore it. C's #include can happen before main, but that does not mean an interpreter must do so.

Outside the syntax, Python is also virtually uncompilable due to weak typing. In C, + has a very limited set of meanings - integer, floating point or pointer, and the compiler will know the static type of the arguments. C++ has far more extensive overloading, but the same basic principle applies. virtual functions allow some run-time flexibility, but from an finite set of options, all compiled before main starts.

Also not syntax is the memory model - both C and C++ have a memory model that's an improved derivative of Java's memory model, which makes threading quite efficient. (Unlike Java, not every object can be synchronized, you need special members). As CPU's gain more and more cores, the advantages only continue to grow. Compilers can see pretty well where memory and CPU registers need to be brought in sync.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thanks to everybody for the answers. I have a better understanding why Python, specifically, cannot be compiled. [I looked up Cython but it seems a bit of a kludge compared to the concept I was asking about.] I may have missed something but it still seems possible to have a language implemented in such a way that an interpreted program could be easily converted to a compiled program. If compiling needs strong typing for variables then require that in the interpreted version, etc. – Salem Engineer Sep 06 '21 at 20:45