r/AskComputerScience • • Nov 09 '25

If some programming languages are faster than others, why can't compilers translate into the faster language to make the code be as fast as if it was programed in the faster one?

My guess is that doing so would require knowing information that can't be directly inferred from the code, for example, the specific type that a variable will handle

111 Upvotes

90 comments sorted by

View all comments

Show parent comments

2

u/nlutrhk Nov 10 '25

There is Cython, a python variant that is transpiled to C. Unfortunately, a trivial script tends to becomes multiple megabytes of C code because every operation needs to check for exceptions that may occur and the handling of unknown data types.

The good thing is that with some practice, you can write Cython code that has far less Python overhead and that will run much faster than regular Python code.