Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sieve of Eratosthenes with closures

by djWestTexas (Initiate)
on May 15, 2018 at 19:09 UTC ( #1214579=note: print w/replies, xml ) Need Help??


in reply to Sieve of Eratosthenes with closures

I know that this is a very old thread, but I just ran across it today while researching Sieve of Eratosthenes implementations. I am awed by the great programming, but much of it is beyond me. However, my main interest was to find a great SoE implementation for use and examples to others. I was really interested in outright performance. With this qualifier in mind, here are a few observations:

The fastest implementation was the c code by AnonymousMonk. However, it isn't perl and has memory limitations for finding larger primes. But it is blazing fast for small lists of primes - 1st two million primes found in less than 1 second.

The fastest perl implementation was the code by tilly. It found all primes up to 2,000,003 in 1 second. It also scaled nearly linearly for up to 32 million primes (the last one I tested). Wow!

The original code by thinker was good. It found all primes up to 2,000,003 in 10 seconds. It didn't scale as well for larger values of primes.

The code by merlynn has some kind of problem. It took it 43 seconds to find all primes up to 2,000,003 and didn't scale well at all for larger values.

Hope my "years late" comment doesn't upset anyone's apple cart. But if anyone else, like me, runs across this thread, it may offer something useful.

If I need to find a bunch of primes quickly, I would use tilly's code!

djWestTexas
  • Comment on Re: Sieve of Eratosthenes with closures

Replies are listed 'Best First'.
Re^2: Sieve of Eratosthenes with closures
by danaj (Friar) on Mar 22, 2019 at 18:00 UTC

    tilly's is a very nice closure implementation. It isn't the fastest pure Perl SoE though. For example, for the first 5761455 primes (primes up to 10^8) tilly's code takes 35.7s on my Macbook. The vector sieve on RosettaCode here takes 37.9s, and the string sieve here takes only 10.2 seconds, albeit with more memory and not an iterator. Using the pure-Perl segmented sieve in Math::Prime::Util::PP takes 7.1 seconds. All of these are terrifically slow compared to C.

    If you're looking for even faster SoEs, I recommend Prime Sieve Benchmarks for a list of benchmarks. The C code from AnonymousMonk back in 2003 works fine, but is about 2x slower than the "Trivial 4-line SoE" shown on that page, because it doesn't get the inner loop start correct. Of course they're all in C, though one is built into a Perl module. In general primesieve is the fastest and easiest solution for most projects.

    To compare using XS modules, printing primes to 10^8. At this point a majority of our time is actually spent printing, which is why the specialized "print_primes()" routine stomps on everything else, since it has optimized C code (even C's printf is a major bottleneck once the sieving is fast enough).

    • 3.4s Math::Prime::XS
    • 2.8s Math::Prime::FastSieve
    • 2.0s Math::Prime::Util "say for @{primes(1e8)}"
    • 1.9s Math::Prime::Util "forprimes { say } 1e8"
    • 0.15s Math::Prime::Util "print_primes(1e8);"

    Slightly better, here we're naively counting, except for the last one which uses a fast exact prime count method.

    • 1.7s Math::Prime::XS
    • 0.89s Math::Prime::FastSieve
    • 0.39s Math::Prime::Util "$n++ for @{primes(1e8)}"
    • 0.18s Math::Prime::Util "forprimes { $n++ } 1e8;"
    • 0.00026s Math::Prime::Util "say prime_count(1e8)"

Re^2: Sieve of Eratosthenes with closures
by erix (Prior) on May 16, 2018 at 08:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214579]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-05-17 01:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?