in reply to performance of perl vs java
In the vast majority of cases, barring painstaking optimization, you will probably find that selecting an appropriate algorithm will have a much larger effect on program run time than the language the program is written.
Not particularly tuned; tested on a 6 year old computer:
use warnings; use strict; use Time::HiRes qw/ tv_interval gettimeofday/; my $time0 = [gettimeofday]; use warnings; use strict; my $max = 100; my @primes = ( 2, 3 ); #seed the primes my $this = $primes[-1]; while ( $max > scalar @primes ) { $this += 2; my $sqrt = $this**.5; for (@primes) { if ( $_ > $sqrt ) { push @primes, $this; last; } next if $this % $_; last; } } my $elapsed = tv_interval( $time0, [gettimeofday] ); print "Printing the first $max numbers that are prime... \n"; print "$_ is prime.\n" for @primes; print "Took $elapsed seconds.";
Printing the first 100 numbers that are prime... 2 is prime. 3 is prime. 5 is prime. ... ... 521 is prime. 523 is prime. 541 is prime. Took 0.00096 seconds.
|
|---|