I'm not a programmer, but I often write a script to extract specific data from numerical simulation results and to make some input sequence files for simulation. When it comes to this type of work, Perl seems to be better than others according to web evaluation.
One day, I heard news that the longest prime number was found by the computer, and I became wondering how long does it take to find prime numbers by a Perl script.
Here is a script to find numerical numbers up to N.
# script to find prime numbers to N
$N=100000;
# 2 is an exceptional number to this script
$numP=1;
# start to find prime numbers from 3for ( $k=3;$k<=$N;$k+=2) {
$m=2;
until ( $k%$m==0) {
$m++;
}
if ( $k==$m) {
$numP++;
}
}number to $N is $numP\n";
In this script, I take the number 2 as an exceptional one, and try to find prime numbers from 3 in for-loop. For each $k over 3, modulo is performed from 2 to $k until 0 is found.
I compared the process time of three computers by using build-in function "time" in Linux.
As is set above script, I run the script to find numbers up to 100,000. Here are comparison results.time perl scriptname . pl
8 minutes -- Raspberry Pi 2 model B
36 seconds -- Mac book pro 2012
17 seconds -- Company Linux machineThis makes sense to me in terms of computer spec. Company machine is as powerful as being able to run the advanced spice simulator easily and memory is also over 100G.
When $N increases, then the process time also increases exponentially. Try it.
No comments:
Post a Comment