Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

RE: RE: RE: sieve of erothenes

by maverick (Curate)
on Jul 10, 2000 at 01:05 UTC ( #21721=note: print w/replies, xml ) Need Help??

in reply to RE: RE: sieve of erothenes
in thread sieve of erothenes

Very slick! I've never seen it done like this before. We're not using the same algorithm, but for my own morbid curiosity I benchmarked it against mine and ase's code. I added the 'o' modifier to your regex to speed it up a bit.
#!usr/bin/perl -w use Benchmark; timethese(100,{ Maverick => sub { for (1..1000){ push @_,$_; for (@_[1..$#_-1]){ pop @_ && last if !($_[-1]%$_) } } #print join "\t", @_; }, Ase => sub { $v='1' x 1000; for(2..1000){ next if !vec($v,$_-1,8); for($q=$_*2;$q<1001;$q+=$_){ vec($v,$q-1,8)=0 } } #print join "\t",grep {vec($v,$_-1,8)} (1..100 +0); }, Alan => sub { for(1..1000){ push @_,$_ if (1 x $_) !~ /^1?$|^(11+? +)\1+$/o } #print join "\t", @_; } });
and ended up with:
Benchmark: timing 100 iterations of Alan, Ase, Maverick... Alan: 93 wallclock secs (92.95 usr + 0.00 sys = 92.95 CPU) Ase: 1 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) Maverick: 1 wallclock secs ( 1.65 usr + 0.01 sys = 1.66 CPU)
You have the shortest but ase still has the fastest. :)

This started off being about the seive of "the always mispelled guy", but now I'm curious about other ways of doing this. Perhaps on some rainy afternoon I'll go dig up few more interesting snippets to throw at the monk collective...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://21721]
[LanX]: aha Sandy Koufax is a Jewish hero because he didn't play on Jom Kippur, hence stonecolddevin's Sig is expressing Jewish "pride" ?
LanX atheism is so boring logical...
1nickt loves the two additional downvotes for Re^2: Catalyst:: Dispatcher - usage of expand_actions, lol
[LanX]: your v welcome
[LanX]: I'm curious, what's the problem with this sig?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-05-24 21:31 GMT
Find Nodes?
    Voting Booth?