Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my computation so much faster in C# than Python

Tags:

Below is a simple piece of process coded in C# and Python respectively (for those of you curious about the process, it's the solution for Problem No. 5 of Project Euler).

My question is, the C# code below takes only 9 seconds to iterate, while completion of Python code takes 283 seconds (to be exact, 283 seconds on Python 3.4.3 - 64 bits and 329 seconds on Python 2.7.9 - 32 bits).

So far, I've coded similar processes both in C# and Python and the execution time differences were comparable. This time however, there is an extreme difference between the elapsed times.

I think, some part of this difference arise from the flexible variable type of python language (I suspect, python converts some part of variables into double) but this much is still hard to explain.

What am I doing wrong?

My system: Windows-7 64 bits,

C# - VS Express 2012 (9 seconds)

Python 3.4.3 64 bits (283 seconds)

Python 2.7.9 32 bits (329 seconds)

c-sharp code:

using System;  namespace bug_vcs {     class Program {         public static void Main(string[] args) {             DateTime t0 = DateTime.Now;             int maxNumber = 20;             bool found = false;             long start = maxNumber;             while (!found) {                 found = true;                 int i = 2;                 while ((i < maxNumber + 1) && found) {                     if (start % i != 0) {                         found = false;                     }                     i++;                 }                 start++;             }             Console.WriteLine("{0:d}", start - 1);             Console.WriteLine("time elapsed = {0:f} sec.", (DateTime.Now - t0).Seconds);             Console.ReadLine();         }     } } 

and python code:

from datetime import datetime  t0 = datetime.now() max_number = 20 found = False start = max_number while not found:     found = True     i = 2     while ((i < max_number + 1) and found):         if (start % i) != 0:             found = False         i += 1     start += 1  print("number {0:d}\n".format(start - 1))  print("time elapsed = {0:f} sec.\n".format((datetime.now() - t0).seconds)) 
like image 733
ssd Avatar asked Apr 27 '15 18:04

ssd


People also ask

Why C programming is faster?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

Is C programming fast?

C is not always faster. C is slower than, for example Modern Fortran. C lets pointer aliasing happen, which means some good optimizations are not possible. Particularly when you have multiple execution units, this causes data fetch stalls.

Is C or C++ faster?

Performance-based on Nature Of Language C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.

Why is C so much faster than Python?

C/C++ is relatively fast as compared to Python because when you run the Python script, its interpreter will interpret the script line by line and generate output but in C, the compiler will first compile it and generate an output which is optimized with respect to the hardware.


1 Answers

The answer is simply that Python deals with objects for everything and that it doesn't have JIT by default. So rather than being very efficient by modifying a few bytes on the stack and optimizing the hot parts of the code (i.e., the iteration) – Python chugs along with rich objects representing numbers and no on-the-fly optimizations.

If you tried this in a variant of Python that has JIT (for example, PyPy) I guarantee you that you'll see a massive difference.

A general tip is to avoid standard Python for very computationally expensive operations (especially if this is for a backend serving requests from multiple clients). Java, C#, JavaScript, etc. with JIT are incomparably more efficient.

By the way, if you want to write your example in a more Pythonic manner, you could do it like this:

from datetime import datetime start_time = datetime.now()  max_number = 20 x = max_number while True:     i = 2     while i <= max_number:         if x % i: break         i += 1     else:         # x was not divisible by 2...20         break     x += 1  print('number:       %d' % x) print('time elapsed: %d seconds' % (datetime.now() - start_time).seconds) 

The above executed in 90 seconds for me. The reason it's faster relies on seemingly stupid things like x being shorter than start, that I'm not assigning variables as often, and that I'm relying on Python's own control structures rather than variable checking to jump in/out of loops.

like image 63
Blixt Avatar answered Oct 10 '22 01:10

Blixt