简体   繁体   中英

Why is this prime sieve so slow in Python compared with the same algorithm in Java or C#?

I'm trying to build a sieve for project Euler solutions. I need primes up until about 100M, preferably with option to go much higher.

This implementation I have works fine, but is very slow:

class Primes:
__size = None
__sieve = []
__primes = []

def __init__(self, size):
    self.__size = size
    self.__sieve = [True] * size
    for x in range(2, size):
        if self.__sieve[x]:
            self.foundPrime(x);

def foundPrime(self, x):
    self.__primes.append(x)
    for duplicate in range(2 * x, self.__size, x):
        self.__sieve[duplicate] = False

For a sieve sized 100M, this initialization takes about 70 seconds on my fairly high-end computer. Does anyone know why? Because in Java and C# this took me about 1 second...

So, this post is different from other posts in that I do not want to know how to implement the algorithm, I want to understand why it is so slow in Python.

Some prints give me the information that about 50% of the time is spent on finding the first 100K primes.

In various benchmarks, for what they are worth, Python is anywhere from the same speed to 50 times slower than Java, dependent on the problem. This is largely due to Python being interpreted, and Java being compiled (even if not to native). Ruby posts similar scores as Python.

Language design also gives Java and C# some advantages.

There are two good ways to speed things up, aside of more efficient Python methods: use pypy, which essentially bytecompiles your python, similar to Java, or write the critical sections in a faster language such as C, and call those routines from Python, a task which is actually pretty easy, assuming you are good with the fast language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM