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.


Author: brabster

Software developer in the North of England

5 thoughts on “Perl is slower and faster than Java”

  1. 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. @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.

  3. 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.

  4. @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… 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s