Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: performance of perl vs java

by repellent (Priest)
on Oct 30, 2011 at 05:10 UTC ( [id://934681]=note: print w/replies, xml ) Need Help??


in reply to performance of perl vs java

Perl is a great language to learn, and I hope you'd remain around on PerlMonks to see the cool things you could do with it. It's usually fast enough, but more importantly, it's highly "optimized" for programmer effort such that you can get more done with minimal coding.

Performance comparisons between two language implementations is hard to get right. You need to know the intricacies of both languages to avoid an apples vs oranges comparison.

Here's a different Perl implementation that runs a lot faster on crappy hardware. Rehashing merlyn's article, I got the following code, which uses core Perl features only, to find all primes <= 3_000_000 (216816 total) in 9 secs:
timethis for 1: 9 wallclock secs ( 7.64 usr + 1.22 sys = 8.86 CPU) +@ 0.11/s (n=1)

This is run on FreeBSD vmware (256MB RAM) on an 8 year-old Athlon XP.

use Benchmark qw(timethis); sub primes { my $upper = shift; return () unless defined($upper); $upper = int(abs($upper)); my $sqrt1 = sqrt($upper) + 1; my $sieve = ""; my @p = (2, 3, 5); my $n; my $iter = sub { if (@p) { $n = shift @p; } else { do { # only ends with 1, 3, 7, or 9 $n += ($n % 10 == 3) ? 4 : 2; } while vec($sieve, $n, 1); } return undef if $n > $upper; if ($n < $sqrt1) { for (my $m = $n**2; $m <= $upper; $m += $n) { vec($sieve, $m, 1) = 1; } } return $n; }; return $iter unless wantarray; my @gather; while (defined(my $x = $iter->())) { push @gather, $x; } return @gather; } timethis -1, sub { printf "count: %d\n", scalar(() = primes(3_000_000) +) };

Update: Added preamble for OP.

Replies are listed 'Best First'.
Re^2: performance of perl vs java
by davido (Cardinal) on Oct 30, 2011 at 15:36 UTC

    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

      Thanks for the Inline C approach. That showcases one of Perl's strengths: to be able to dive down lower-level to C (via XS) and glue it back to Perl-land if we need the extra performance, all with minimal effort.

      I'm curious - how long does my Perl primes(3_000_000) take on your machine?

      Also, what are your machine specs?
Re^2: performance of perl vs java
by anneli (Pilgrim) on Oct 30, 2011 at 08:03 UTC

    awesome, well-thought, well-worded post

    What!? You're not a repellent at all, repellent!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-29 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found