Reputation: 324
I am looking to make a python implementation of the sieve of Eratosthenes with a segmented sieve, and using L1 Cache of CPU.
I have my own version on github here: https://github.com/nick599/PythonMathsAlgorithms/blob/master/segmented_soe_v6.py , which does not use L1 cache size of the CPU.
I found the following site - http://primesieve.org/segmented_sieve.html , which gives a C++ implementation using the L1 cache size. It says it is much faster than my algorithm (mine takes several minutes for creating primes upto 10^7, and hangs on 10^8 due to memory usage).
I am developing on Linux Mint v17, python version: 2.74. Update My CPU is an Intel i7.
I am fairly new to python.
I want to know:
Looking for answers that answer the spirit of all my questions above. Hints and tips are welcomed.
Upvotes: 0
Views: 183
Reputation: 5613
I'm not sure you can make enough assumptions about how Python uses memory in order to ensure it efficiently uses the L1 cache. Also, 10^8 is only 1/2 Gig, so your current implementation must be pretty inefficient at element allocation as it stands. You may be better off creating the largest possible string, and indexing that as your sieve storage, rather than using an array of integers if you are only going to store a single flag in each location? It would certainly be possible to use a string as your segmented sieve storage, and if you are lucky, they might be small enough to live on the L1 cache. C has some good bit indexing and manipulation, which I'm sure are available in python to allow you to independently manipulate each bit. You can do bit manipulation on character values.
Upvotes: 1