Reasons Behind the Slower Performance of Python Programs Compared to C or C++ Equivalent Programs

Python’s byte code runs slower than other interpreted languages, such as Java/.NET, because they have a JIT compiler included in their standard distribution that compiles byte code into native code during runtime. Python’s slow speed is primarily due to it being an interpreted language, where its code gets interpreted at runtime instead of being compiled to native code during compile time, unlike native languages such as C/C++.


As I started learning C, I decided to compare its performance with Python. Thus, I wrote a code to iterate through all the 8 character alphanumeric combinations. Surprisingly, the
python code
ran way faster than the C code, although both were quite similar. For the
Python code
I used PyCharm, and for C
code. Running
, I used CLion. Interestingly, the speed remained the same even when I executed the code outside the CLion environment.
exe file


char_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
             'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for char1 in char_list:
    for char2 in char_list:
        for char3 in char_list:
            for char4 in char_list:
                for char5 in char_list:
                    for char6 in char_list:
                        for char7 in char_list:
                            for char8 in char_list:
                                print(char1 + char2 + char3 + char4 + char5 + char6 + char7 + char8)


int main() {
    char charset[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
                      't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    for (int i1 = 0; i1 < sizeof charset; i1++) {
        for (int i2 = 0; i2 < sizeof charset; i2++) {
            for (int i3 = 0; i3 < sizeof charset; i3++) {
                for (int i4 = 0; i4 < sizeof charset; i4++) {
                    for (int i5 = 0; i5 < sizeof charset; i5++) {
                        for (int i6 = 0; i6 < sizeof charset; i6++) {
                            for (int i7 = 0; i7 < sizeof charset; i7++) {
                                for (int i8 = 0; i8 < sizeof charset; i8++) {
                                    printf("%c%c%c%c%c%c%c%cn", charset[i1], charset[i2], charset[i3], charset[i4],
                                           charset[i5], charset[i6], charset[i7], charset[i8]);
    return 0;


Personally attempted, in a
console, measuring
the duration required for the initial ten million lines.


$ time python3 | head -10000000 | tail
real    0m6.075s
user    0m6.246s
sys     0m0.262s

Both compilation methods, as stated in the initial response (which has since been removed), were utilized for C.

$ gcc main.c -o main
$ time ./main | head -10000000 | tail
real    0m2.140s
user    0m2.350s
sys     0m0.300s
$ gcc -O3 -Ofast -Og -Os -funroll-loops -s main.c -o main
$ time ./main | head -10000000 | tail
real    0m2.119s
user    0m2.278s
sys     0m0.347s

Based on my analysis, it seems that Python isn’t 20 times quicker than this, but in fact, slower. It appears that the sluggish speed you experienced was due to the consoles used to run the C program.

Frequently Asked Questions