If Python was coded(based) on C then can Python ever surpass the C? I know that the next stages be assembly, binaries when they communicate with OS and hardware. I have two assumptions that since most of the Operating Systems were coded in C then if the any code works on top of that OS, it is not possible that Python can be faster.
-
1The fact that the OS is coded in C does not have anything whatsoever to do with this. – klutt Apr 15 '21 at 18:10
-
1I think you would be hard pressed to make python run faster than C. – S R Maiti Apr 15 '21 at 18:10
-
3If the C program is poorly written, I can see how a Python program could be faster. – fuz Apr 15 '21 at 18:11
-
1And it does not matter if the Python interpreter is coded in C either. C (and Python) is turing complete. – klutt Apr 15 '21 at 18:11
-
1@klutt: The fact that a Python implementation is coded in C implies that any algorithm implemented in that Python implementation can be implemented in C at least as fast, and therefore Python can outperform C only by comparing a good algorithm implementation in Python to a suboptimal C implementation. Turing completeness is irrelevant. – Eric Postpischil Apr 15 '21 at 18:20
-
@EricPostpischil Yep, that's a point! My point is that if they would perform the same target of program then can Python outperform in terms of structure(the way they communicate with hardware and OS)? – Mark Ezberg Apr 15 '21 at 18:25
-
@EricPostpischil How do you mean? You can write a C compiler in Python if you want. – klutt Apr 15 '21 at 18:26
-
1@klutt: Let X be a Python program that is executed in a Python implementation Y, where the Python implementation Y is written in C and executed in a C implementation Z. Let W be the performance-optimal (by whatever measurement is preferred) C program that performs the same task as X. Then it is impossible for X to be faster than W, because the combination of X+Y is a C program that performs the same task as W, and therefore, if X+Y is faster than W, then W is not the optimal C program that performs the same task as X. Whether a C compiler can be written in Python is irrelevant. – Eric Postpischil Apr 15 '21 at 18:42
-
@klutt You can write a *compiler* in python, but compilation time has nothing to do with execution time. – Eugene Sh. Apr 15 '21 at 18:49
-
@EricPostpischil Ok, sure. I realize I phrased everything wrong. What I was trying to say (but failed completely with) is that a Python interpreter does not *need* to be coded in C. – klutt Apr 15 '21 at 18:55
1 Answers
All things being equal, code running in an interpreter will execute more slowly than code running natively. However, things are rarely equal, and while I can't think of an example offhand, I would not be surprised if there were circumstances where a Python solution could execute faster than a C-based one (it'd probably be pretty esoteric, though).
Beyond that, raw execution speed is only one metric, and it's not the most important. It doesn't matter how fast your code is if it does the wrong thing, or nukes a server if someone sneezes, or exposes your system to malware, or it takes you a year to deliver a solution.
Python provides a bunch of high-level abstractions and tools that C doesn't, leading to faster development time (which is where the expense really is). You don't have to worry (as much) about memory leaks, buffer overruns, etc.
There is no such thing as a silver bullet, and no language is best at all things. There are times when a C-based solution is the right answer, and there are times when a Python-based solution is the right answer.

- 119,563
- 19
- 122
- 198
-
-
Another way to put this is that a Python program may be faster if you can write it and run it in less time than it takes to write the C program and compile it without errors. – prl Apr 16 '21 at 01:00
-
Python can be faster if your C program uses simplistic algorithms like a hand-written Insertion Sort on a large array, instead of a library sort, or manual inefficient data structures / algorithms instead of a Python hash table. Or other terrible things like manually calling `read` 1 char at a time instead of using I/O libraries that make things easier. And for good measure, compile your C with optimization disabled. (And other diabolically-incompetent measures from [Deoptimizing a program for the pipeline in Intel Sandybridge-family CPUs](https://stackoverflow.com/q/37361145) :P) – Peter Cordes Apr 16 '21 at 03:28
-
Since higher-level languages (and even C++) provide easier access to good canned algorithms, people are more likely to use them instead of (in C) rolling their own of some simple idea that might involve double or triple nested loops (e.g. traversing an array for every element of another array). So yeah it's totally possible to write slow C, especially if you want to try to compete with Python's speed of code-writing by *not* spending time finding good algorithms and implementations of them. – Peter Cordes Apr 16 '21 at 03:31