Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

It includes clever use of the vec operator.

Maybe there's historical reasons for using vec over substr, but nowadays it's much faster to use substr. The for my $foo ($from .. $to) { ... } construct is also slightly more efficiently than for (my $foo = $from; $foo <= $to; $foo++) { ... }. My guess is that the C-style loop was used as the "range loop" wasn't optimized back then and actually generated a list of, in this case, a million numbers.

I changed the for loop and did a direct translation to using substr instead of vec, and here's the results:

Benchmark: running substr, vec for at least 60 CPU seconds... substr: 61 wallclock secs (62.41 usr + 0.02 sys = 62.42 CPU) @ 1 +.09/s (n=68) vec: 58 wallclock secs (60.64 usr + 0.02 sys = 60.66 CPU) @ 0 +.73/s (n=44) s/iter vec substr vec 1.38 -- -33% substr 0.918 50% --
Here's the code:
use strict; use warnings; use Benchmark qw(cmpthese timethese); my %subs; ###################################################################### +######### my $UPPER = 1_000_000; $subs{vec} = sub { my $sieve = ""; GUESS: for my $guess (2 .. $UPPER) { next GUESS if vec($sieve,$guess,1); #print "$guess\n"; for (my $mults = $guess * $guess; $mults <= $UPPER; $mults += +$guess) { vec($sieve,$mults,1) = 1; } } }; $subs{substr} = sub { my $sieve = '0' x ($UPPER + 1); GUESS: for my $guess (2 .. $UPPER) { next GUESS if substr($sieve,$guess,1); #print "$guess\n"; for (my $mults = $guess * $guess; $mults <= $UPPER; $mults += +$guess) { substr($sieve,$mults,1,1); } } }; ###################################################################### +######### cmpthese(timethese(-60, \%subs));

lodin


In reply to Re^2: To Findout Prime number by lodin
in thread To Findout Prime number by babug_prg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (13)
    As of 2014-12-20 17:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (96 votes), past polls