But this will find 3,000,000 over six times in a second:

use strict; use warnings; use feature 'say'; use Benchmark qw/timethis/; use Inline 'C'; my $search_to = 3000000; timethis -10, \&eratos_inline_c; # Thin wrapper to bind input params for the benchmark. sub eratos_inline_c { return @{inline_c_pa_eratos_primes( $search_to )}; } __DATA__ __C__ /* Find all primes up to 'search_to' using the Sieve of Eratosthenes. +*/ AV * inline_c_pa_eratos_primes ( int search_to ) { AV* av = newAV(); int* primes = 0; int i; Newx( primes, search_to + 1 , int ); if( ! primes ) croak( "Failed to allocate memory.\n" ); for( i = 0; i <= search_to; i++ ) primes[i] = 1; for( i = 2; i * i <= search_to; i++ ) { if( primes[i] ) { int j; for( j = i; j * i <= search_to; j++ ) primes[ i * j ] = 0; } } for( i = 2; i <= search_to; i++ ) { if( primes[i] == 1 ) av_push( av, newSViv( i ) ); } Safefree( primes ); return sv_2mortal( av ); }

Dave


In reply to Re^2: performance of perl vs java by davido
in thread performance of perl vs java by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.