@kkarhan ok so first i just want to say I know that writing stupid little scripts to find prime isn’t exactly productive, but it is fun to test and prod the system. I decided to write a similar set of functions in perl to what I wrote in javascript to see a side by side comparison and I am very sad to say that perl was absolutely killed by node in this test. There may be ways to optimize the perl code, but it is currently more or less the same as the javascript posted before.
[john@katana ~]$ node primes.js prime-sieve: 1.146s Found {10000} primes with the largest prime = {104729} [john@katana ~]$ perl primes.pl 10000 Found {10000} primes in 27.40608 seconds with the largest prime = {104729}Here is the perl code I used for this test. I expected at least similar results to javascript. I was very surprised to see such a difference. I imagine there are some obvious explanations for this if I understood the lower level components more, but it’s funny because I often joke about how bad javascript is at math :shrugz:
#!/usr/bin/env perl use warnings; use strict; use Scalar::Util qw(looks_like_number); use Time::HiRes qw(gettimeofday tv_interval); sub isPrime { my ( $input ) = @_; return if ( !looks_like_number($input) || $input < 2 || ($input != 2 && $input % 2 == 0) ); for (my $chkval = 3; $chkval < $input; $chkval++) { return if ($input % $chkval == 0); } return 1; } sub getNextPrime { my ( $start ) = @_; $start = 1 if (!$start || !looks_like_number($start)); my $prime; do { $prime = $start if (isPrime(++$start)); } while (!$prime); return $prime; } sub findPrimes { my ( $count ) = @_; my @primes; while ($#primes + 1 < $count) { push(@primes, getNextPrime($primes[@primes - 1])); } return @primes; } my $t0 = [gettimeofday()]; my @primes = findPrimes(@ARGV); my $primes = @primes; my $elapsed = tv_interval($t0); print "Found {$primes} primes in $elapsed seconds with the largest prime = {$primes[@primes - 1]}";