Perl is slower and faster than Java

Bit of a random one coming up…

I needed to get an measure of the difference in performance between Perl and Java for a simple client application, so I wrote the traditional ‘Hello World’ app in both and ran a bunch of executions averaging over the time from start to end of execution. The net result:

Perl is around 34 times faster than Java.

Really? I thought Java was supposed to be fast? In fact, Dhananjay Nene talked about how comparatively fast a selection of languages – including Java – were on his blog, and cwilbur ‘s comments suggest that in that experiment:

Java is around 100 times faster than Perl.

So how can these two conflicting results both be true? I’m sure you’ve already figured it out, but I’m going to tell you anyway.

Java code takes hundreds of milliseconds to start up, because Java code runs in the Java Virtual Machine which needs a little time to get itself ready before your code can run. Once it’s up, however, you can get performance close to (or even better than, in reality) that of typical C++.

On the other hand, Perl doesn’t need that environment and so can start faster but trades off more overhead at runtime, meaning slower running performance.

Just goes to show, application performance is another area where there’s no one right answer – it’s about choosing the right tool for the job – I don’t think there’s any way round these performance characteristics without cheating.

The views expressed here are the personal views of the author.

Posted on January 9, 2010 at 12:39 am by Paul Brabban · Permalink
In: Development, Java, PERL

5 Responses

Subscribe to comments via RSS

  1. Written by Rob A
    on January 9, 2010 at 8:11 am
    Permalink

    So if I understand your post correctly PERL is more appropriate for low frequency code runs and java for high frequency…. or have I missed the point completely… highly probable.

  2. [...] This post was mentioned on Twitter by Mike Croucher, brabster. brabster said: New Blog Post, Perl is slower and faster than Java: http://2tu.us/1ddc [...]

  3. Written by Paul Brabban
    on January 9, 2010 at 12:29 pm
    Permalink

    @Rob A
    Hey Rob,

    Sorta, yes, but it’s more about what proportion of time in a ‘run’ is spent doing useful work.

    Say for example you’re writing some code to run as a command line application to grab the content from a URL and write it to a file. You might expect the app to take a couple of hundred milliseconds to execute, so doing it in Java and incurring several hundred ms at to start would mean much more time taken starting the JVM than running the app.

    On the other hand, if you’re writing the web application behind that URL, it’d make sense to be running already, waiting for requests. In this case, the startup time doesn’t matter so much, as you might spend days, weeks, months running, until you get shut down.

    Somewhere inbetween, say you’re writing a command line app to do some complex processing to a load of scientific data. In this case, you’ll want something that can do your computation as quickly as possible and again, you’ll happily trade off a few hundred ms starting up for that runtime speed.

  4. Written by Mike Croucher
    on January 11, 2010 at 11:35 am
    Permalink

    Often, when I am thinking about which language to use for a particular project, I don’t worry about execution time too much…at first!

    For many of the things that I do it is the development time that is the limiting factor – how long will it take me to go from an idea in my head to something running on my machine?

    Scripting languages such as Python or Perl (and also, for me, Mathematica and MATLAB) tend to be my weapons of choice for this very reason. My viewpoint is probably coloured by the fact that I tend to wrote lots of small demonstrations rather than enterprise level applications though.

  5. Written by Paul Brabban
    on January 11, 2010 at 7:38 pm
    Permalink

    @Mike Croucher
    I agree, an ideal use case for scripting languages is the exploratory, prototyping stuff where you maybe don’t need to worry about speed much.

    On the other hand, I’ve heard you talking about vectorising MATLAB programs to squeeze down your computation time… :)

Subscribe to comments via RSS

Leave a Reply