aspose file tools*
The moose likes Performance and the fly likes Java vs. Perl (Speed) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Java vs. Perl (Speed)" Watch "Java vs. Perl (Speed)" New topic
Author

Java vs. Perl (Speed)

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi,
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:

And here is the listing for Java:
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The link I gave in this thread also compares Java and Perl.
You might find the Methodology section useful, since he picks hole in the creation of his own benchmark statistics, including stuff like this (related to PHP, but could be similar to your situation):
Some languages are not tested on their strengths, but mostly on their weaknesses. Case in point: PHP. PHP is a fine web scripting language that provides a multitude of built-in convenience functions to simplify writing code for common CGI tasks. Since this shootout is a basic language test, and I don't have or plan to have any CGI scripting tests, the fact that PHP is somewhat slower in my tests than other scripting languages is hardly an argument against its use as a web scripting language.

Dave.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Thanks for the link. It looks very interesting. For the nested loop test, the results are much slower than Java which is also what I see in my test.

thanks,
Barry
Kwok Locke Chung
Greenhorn

Joined: Jul 04, 2009
Posts: 1
Barry Andrews wrote:Hi,
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:

And here is the listing for Java:


if you write it in this way,
it run faster.

in the perl internal, the "for" loop will do differently if you write it in this way,
there are still some ways to make perl run faster without changing this algorithms.

HOPE THIS HELP
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16236
    
  21

Unlike python, I don't know of a Perl implementation that caches compilations. So everytime you run a Perl program, the first thing that it has to do is a full parse and compile. Java, in contrast, has a lot of the dirty work already done for it by javac.

Perl is also I think more prone to do on-the-fly interpreting, just due to the language syntax and how you use it. Mileage on this may vary depending on how smart the Perl compiler is.

Perl6 promises all sorts of performance improvements, but Java's had some of the heavyweights in the field working on run-time optimization for a long time now.

Still, I don't expect to stop using Perl anytime soon. Some things aren't worth the extra effort to do in Java.


Customer surveys are for companies who didn't pay proper attention to begin with.
Miguel Carrasco
Greenhorn

Joined: Aug 16, 2011
Posts: 2
There is an error in the java code...

line 28 must be:


If you add...


at the beginning of the perl code, you get more accurate measures!!
I performed the test in java and perl codes and found the following results:

Java code took 27 milliseconds.
Perl code took 1.18756604194641 seconds.

Java seems to be faster than perl after all!!!

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
To be concise, the application in this post written in Java may perform faster that the application written in Perl.
These are simple tasks and are not completely representative of how the technologies are used in the real world.

A better test would include complex string manipulations with pattern matching of 5 GB of records. Here most likely Perl would be faster.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Besides, the original post is now eight years old and is talking about computing environments which are completely different from what we use today.
Miguel Carrasco
Greenhorn

Joined: Aug 16, 2011
Posts: 2
Jimmy Clark wrote:To be concise, the application in this post written in Java may perform faster that the application written in Perl.
These are simple tasks and are not completely representative of how the technologies are used in the real world.

A better test would include complex string manipulations with pattern matching of 5 GB of records. Here most likely Perl would be faster.


I completely agree with you, this test doesn't show the true strengths of Perl and Java in a real application.

Programming languages ​​are meant to solve different types of problems, there isn't something like a best programming language, each programming language has its own advantage and disadvantages.

By the way, I love perl, especially by its simplicity and efficiency to handle regular expressions, but I'm still surprised that Java has a clearly superior performance in this kind of tests.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
How about a Forum feature that locks a post if it is at least 6 months old?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java vs. Perl (Speed)