Why does a large for loop with 10 billion iterations take a much longer time to run in Python than in C?

  • A+

I am currently comparing two loop calculation in Python3 and C. For Python, I have:

# Python3 t1 = time.process_time() a = 100234555 b = 22333335 c = 341500 for i in range(1, 10000000001):     a = a - (b % 2)     b = b - (c % 2) print("Sum is", a+b) t2 = time.process_time() print(t2-t1, "Seconds") 

Then in C, I do the same thing:

#include <stdio.h>  int main() {    long long a = 100234555;    long long b = 22333335;      long long c = 341500;    for(long long i = 1; i <= 10000000000; i++){         a = a - (b % 2);         b = b - (c % 2);    }    printf("Sum is %lld/n", a+b);    return 0; } 

I timed both the code in Python and in C. The timing for Python is around 3500 seconds while the timing in C (including compilation and execution) only takes around 3 seconds.

I am wondering how there is such a big difference in timing. The execution was done on a server with 100 GB Ram and enough processing power.


It's partially due to the fact that Python byte code is executed by a program instead of the CPU directly, but most of the overhead is caused by the memory allocation and deallocation caused by the immutability of integers which is due to the object model, not the interpretedness.

What's going on is that your C code can change the value of the numbers, but in Python numbers are immutable which means they do not change. This means that when you do a sum, Python has to create a new int object for each new value, and then destroy the old ints after they're no longer used. This makes it much slower than just modifying a single memory value.

There is also the possibility that your C compiler is being clever, and reasons that via a chain of optimisations it can completely remove your for loop, and the result will be identical – as if the loop had actually run. I'd expect the code to run much faster than it did if that had been the case in your example, but it could do that.

Python has no such smart compiler. It can't do something as grand and clever as that; it's just not designed to optimise the code because it's so hard to do so reliably in a dynamically-typed language (though the fact that Python is strongly-typed does make it somewhat of a possibility.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: