• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java vs. Perl (Speed)

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Miguel Carrasco
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a Forum feature that locks a post if it is at least 6 months old?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic