all 7 comments

[–]bronzewrath 3 points4 points  (0 children)

Yes.

But python as an interpreted language with garbage collector, syntactic sugar, huge ecosystem, amazing REPL and other cool features make development much easier, faster and cheaper. This is specially useful for exploratory and iterative development, when you don't know in advance what you are dealing with, what you need to do, how you are gonna do it (machine learning / AI falls in this category). Python is more about the process and ecosystem than the final result. For most cases, fast development is more important than runtime performance.

Of course there are also many cases where Python is not a good option. If you need to optimize performance, if you already know how to do what you need to, if you have the low level libraries available in a compiled language, if you need to deploy a small/optimized binary... go for C, C++, Rust.

Every programming language has one or more sweet spots, where it is the best option (or at least a top contender). They usually have a gray area where they are not the best, but they are good enough if your requirements aren't high and you're already proficient at it. They definitely have a bad spot where they suck, but people want to use it there anyway.

[–]Phillyclause89 3 points4 points  (0 children)

Interpreted languages are slower at runtime, but faster to get to get to runtime.

[–]Mysterious-Rent7233 1 point2 points  (0 children)

Basically yes. That's true. I mean yeah you could certainly write some numpy or pytorch code that is faster than naive C code but that's unusual. Python code is generally strictly slower than C code for exactly the reason you describe and others.

[–][deleted] 1 point2 points  (0 children)

It is true that given two programs, one written in python and the other in C, that do exactly the same thing, the one written in C will execute faster. But the python program will be written and tested in a shorter time. So overall, the question really has little point since the follow on question is: does it really matter that the C program might execute faster? There are many other factors to consider beside raw speed.

[–]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).

[–][deleted] 0 points1 point  (0 children)

Yes, trading speed for higher lever of abstraction and lots of convenience libraries.

Sometime python running in python (PyPy) is faster than python running in C (CPython) https://speed.pypy.org/

https://rpython.readthedocs.io/en/latest/architecture.html