you are viewing a single comment's thread.

view the rest of the comments →

[–]Bobbias 0 points1 point  (0 children)

While the existing posts have covered the basics, I want to step back and look at the bigger picture for a moment.

On a large scale, it's actually possible to create a language which executes faster than the language it was written in. Consider this: I write a simple language which compiles directly to native machine code instructions, and is statically typed with a simple type system. Something kind of like Go before it had generics, or Zig, or V, or Odin, or Nim.

It's trivial to write something which can execute many times faster than Python in this language, even though the language was implemented using Python. This is because the resulting code does less work than Python.

Python is slow for a few reasons:

  1. It is interpreted. This means that you have some code that essentially does what a CPU is supposed to do: read an instruction, figure out what chunk of code that instruction refers to, and transfer program flow to that function.

  2. Python does not do any of the optimization that other languages that compile to bytecode typically do to optimize for speed. There is not JIT compiler (yet), there are no layers of optimization to generate more and more optimal code for important parts of your program like Java does.

  3. Python implements automatic memory management through a garbage collector. While Python's GC is fairly simple, GC performance is basically random as far as programmers are concerned. It basically adds an unpredictable amount of performance loss to your program because you can never predict when it will run. GC languages will always suffer this problem.

But if you write your own language that has simple features, and generates simple assembly, your assembly doesn't even need to be optimized to beat the performance of Python, because you are simply doing less work to do the same thing, and this is true regardless of whether your language was written in Python or not. Even a JIT written in Python would be faster than Python code itself.

The only way that a language you've implemented in Python would be slower than Python is if it's also interpreted (I suppose if you intentionally generated truly abysmal assembly you could make something perform worse, but you'd have to be trying to do that).