I am beginning to learn some Perl, and I decided to do a little performance comparison between Java and Perl. I took an example from Sam's Teach Yourself (Listing 3.3) which is the classic 'print the primes' program, and I modified it to display the execution time. Then I made a few more changes so it would run as a Java program. To my surprise, Java was MUCH faster than Perl. In Java, to print out the first 100 prime numbers took only milliseconds, but the same program in Perl took 10 seconds. HUGE difference! I thought that Perl would be much faster, but I guess I was wrong. Can anyone provide some feedback on this? Here is the code for Perl:
$maxprimes=100; $value=1; $count=0; $start=time(); print "Printing the first $maxprimes numbers that are prime... \n" +; while ($count < $maxprimes) { $value++; $composite=0;#false OUTER: for ($i=2; $i < $value; $i++) { INNER: for ($j=$i; $j<$value; $j++) { if (($j*$i) == $value) { $composite=1;#true last OUTER; } } } if (! $composite) { $count++; print "$value is prime\n"; } } $time = (time() - $start); print "Took $time seconds.";

And here is the listing for Java:

public class Listing3_3 { public static void main(String[] args) { int $maxprimes=100; int $value=1; int $count=0; long $start = System.currentTimeMillis(); System.out.println("Printing the first " + $maxprimes + " +numbers that are prime... \n"); while ($count < $maxprimes) { $value++; int $composite=0;//false OUTER: for (int $i=2; $i < $value; $i++) { INNER: for (int $j=$i; $j<$value; $j++) { if (($j*$i) == $value) { $composite=1;//true break OUTER; } } } if ($composite == 1) { $count++; System.out.println($value + " is prime\n"); } } long $time = (System.currentTimeMillis() - $start) / 1000; + System.out.println("Took " + $time + " seconds"); } }

In reply to 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.