简体   繁体   中英

Python implementation of c++ algorithm using L1 CPU Cache

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:

  1. How I could start implementing a python version of this C++ algorithm?
  2. What I would need to consider?
  3. Are there things in the C++ implementation that can't be coded in Python 2.74?
  4. What about multithreading?
  5. What about hyperthreading?
  6. What about python's GIL?

Looking for answers that answer the spirit of all my questions above. Hints and tips are welcomed.

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.

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