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

ssd :

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))
Blixt :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is this code in Python so much faster than in C++?

Why is strcmp so much faster than my function?

Why is awk so much faster than python in this case?

Why is an array so much faster than an ArrayList?

Why is sum so much faster than inject(:+)?

Why is any() so much faster than in?

Why is C++ much faster than python with boost?

why "in" generator is much faster than "in" list in python

why < is much faster than !=?

Why is PHP7 so much faster than Python3 in executing this simple loop?

Why is string format so much faster than converting something into a string in Python?

How can numpy be so much faster than my Fortran routine?

Why is numpy's cumsum so much faster than manual C++'s loop?

Why is this JOML (JVM) code so much faster than the equivalent GSL (C)?

Why does my python script run much faster when using 5 CPUs rather than 12?

Why is async code considered so much faster than synchronous?

Why is memcmp so much faster than a for loop check?

Why is linear search so much faster than binary search?

Why is Racket implementation so much faster than MIT Scheme?

Why is a list comprehension so much faster than appending to a list?

How does COPY work and why is it so much faster than INSERT?

Why is Eigens mean() method so much faster than sum()?

Why is batch mode so much faster than parfor?

Why is pow(a, d, n) so much faster than a**d % n?

Why is pow(a, d, n) so much faster than a**d % n?

Why is it so much faster passing an array as a literal rather than a parameter?

Torch: Why is this collate function so much faster than this other one?

Why is filtering DataFrame by boolean mask so much faster than apply()?

Why items method for dictionary is so much faster than a straightforward iteration?