performance of large number calculations in python (python 2.7.3 and .net 4.0)

Posted by g36 on Stack Overflow See other posts from Stack Overflow or by g36
Published on 2013-05-02T17:23:06Z Indexed on 2013/06/25 22:21 UTC
Read the original article Hit count: 315

Filed under:
|

There is a lot of general questions about python performance in comparison to other languages. I've got more specific example: There are two simple functions wrote in python an c#, both checking if int number is prime.

python:

import time

def is_prime(n):
    num =n/2
    while num >1:
        if n % num ==0:
            return 0
        num-=1
    return 1

start = time.clock()
probably_prime = is_prime(2147483629)
elapsed = (time.clock() - start)
print 'time : '+str(elapsed)

and C#:

 using System.Diagnostics;

 public static bool IsPrime(int n)
        {
            int num = n/2;
            while(num >1)
            {
                if(n%num ==0)
                {
                    return false;
                }
                num-=1;
            }
            return true;
        }
 Stopwatch sw = new Stopwatch();
 sw.Start();
 bool result = Functions.IsPrime(2147483629);
 sw.Stop();
 Console.WriteLine("time: {0}", sw.Elapsed);

And times ( which are surprise for me as a begginer in python:)):

Python: 121s; c#: 6s

Could You explain where does this big diffrence come from ?

© Stack Overflow or respective owner

Related posts about python

Related posts about .NET