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))
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.
Comments