Answer the question
In order to leave comments, you need to log in
Why does it seem to me that Python is faster than C?
Wrote two programs. One is in C and the other is in Python.
Both are identical in meaning. The meaning is this. There is a variable i. In an infinite loop, it is displayed on the screen and then incremented.
C, as I understand it, is considered a fast language. Python is otherwise considered a slow language. Although some say that it is not.
It is not clear in the end what is still faster.
I understand that this cycle is not an indicator of speed, but still I wonder why this happens.
Here is the C code:
#include <stdio.h>
int main()
{
int i = 0;
while(1 == 1) {
printf("%i\n", i);
i += 1;
}
scanf("%i", &i);
return 0;
}
i = 0
while True:
print(i)
i += 1
Answer the question
In order to leave comments, you need to log in
It has long been known that synthetic tests only measure the speed of the synthetic tests themselves.
In short - in theory, with the correct writing of faster human-written code in C, there can only be code written by a person directly in ASMA. In practice - asma code is likely to be slower - the C compiler knows a lot of things that a person may not know / not take into account, so it will generate code more efficiently.
On this occasion - there are too many questions)
What axis? How did you collect the sish code? Maybe you compiled c code in debugging?)
Well, here is an example of ubuntu (python3 vs gcc)
Since you are talking about a noticeable difference at 300,000 iterations, let's just set the limit to 500,000 and compare with the time utility:
#include <stdio.h>
int main()
{
int i = 0;
while(i != 500000) {
printf("%i\n", i);
i += 1;
}
return 0;
}
i = 0
while i != 500000:
print(i)
i += 1
$time python3 test.py
...
499999
real 0m6.472s
user 0m0.985s
sys 0m1.063s
$time ./a.out
...
499999
real 0m3.241s
user 0m0.234s
sys 0m0.749s
Here, the bottleneck is the function printf()
, and it's not just about output to the console or somewhere else, but also about parsing the format string on each repetition and converting the number to a string.
Well, not only are the programs not identical, and it’s not clear with what options it was compiled / run, but also the output to the console ...
The output itself can slow down, and its IMPLEMENTATION does not in any way reflect the speed of the LANGUAGE.
I understand that this cycle is not an indicator of speed, but still I wonder why this happens.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question