S
S
SKEPTIC2020-03-02 22:33:47
Python
SKEPTIC, 2020-03-02 22:33:47

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;
}


Here is the Python code:
i = 0
while True:
  print(i)
  i += 1

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Gornostaev, 2020-03-03
@pro100chel

It has long been known that synthetic tests only measure the speed of the synthetic tests themselves.

M
menkar3, 2020-03-02
@menkar3

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;
}

Against
i = 0
while i != 500000:
  print(i)
  i += 1

Let's see what happened:
$time python3 test.py 
...
499999
real	0m6.472s
user	0m0.985s
sys	0m1.063s

6 cents seconds. Now let's compare with the c solution, which we compile by default gcc test.c:
$time ./a.out 
...
499999
real	0m3.241s
user	0m0.234s
sys	0m0.749s

3 cents of a second.
It already somehow diverges from your observations, so the question about the system / compiler / assembly parameters of the C code is relevant)
Not to mention the fact that (as mentioned above) speed measurements by output to the console are generally a bad idea

W
wisgest, 2020-03-02
@wisgest

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.

G
GavriKos, 2020-03-02
@GavriKos

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.

V
Vitaly, 2020-03-02
@vt4a2h

I understand that this cycle is not an indicator of speed, but still I wonder why this happens.

If you're really interested, use profilers to find out which part of the program is taking what time to complete. Then play with C compilation flags and Python implementations. Next, evaluate the influence of the environment.
Oh yeah, and it would be nice to figure out what exactly you are trying to check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question