Wednesday, August 29, 2012

Java: Helloooooooo, World!

I was talking to my buddy, Peter von der Ahé, today about optimizing startup time. He asked me to guess how many classes the JVM had to load for a simple hello world application written in Java. He also asked me how long such a program would take to run. I didn't know the answers, so he quickly typed it out on my friend's MacBook Pro.

Here's how to see how many classes are loaded:

java -verbose:class Hello

That results in 594 classes:

[Opened /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.Object from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.io.Serializable from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.lang.Comparable from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
...
[Loaded java.util.Collections$UnmodifiableMap from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]

It takes roughly 0.2 seconds to run hello world:

time java Hello
Hello, World!

real 0m0.212s
user 0m0.221s
sys 0m0.051s

Kind of interesting. I know that this is the world's stupidest benchmark, but I decided to write the same thing in Python. It takes 0.04 seconds.

Dart currently takes 0.02 seconds.

Ruby 1.8.7 takes 0.009 seconds.

C takes 0.006 seconds.

(Warning: bad jokes coming...)

Of course, the real reason C is so fast is because it doesn't have any class ;-)

As I said earlier, this is a totally stupid benchmark. Never pick a programming language based on how long it takes to run hello world. What happens if you have to write a program that says something other than hello world, such as "hej, verden"? ;-)


8 comments:

rog said...

On my machine, Go: 0.007s

Go has no class either :-)

Marius Gedminas said...

It may be a stupid benchmark, or it may be a real scalability issue, in some rare cases.

I wish I had a link ready (or better Google skills), but I remember a blog post (or maybe it was a video of a talk? a live talk at EuroPython 2012?) by someone from Github about a performance problem they've identified. Turns out they used a Python script as a shell for ssh logins that did some checks and spawned git. The time to import all of Django's ORM was maybe 0.5 seconds, but when they had 50 incoming SSH connections a second, it all added up to quite a hefty load.

Shannon Behrens said...

Marius, that's a funny story. Spawning off a Python shell script certainly qualifies as scalable (since you can just throw more hardware at the problem), but it's certainly not performant (0.5 seconds!) or efficient (that's really expensive in terms of memory).

Anonymous said...

For my linux desktop:

>time java -cp . HelloWorld -verbose:class
Hello World

real 0m0.055s
user 0m0.044s
sys 0m0.011s

and loaded 314 classes.

and for python:

>time python HelloWorld.py
Hello World

real 0m0.022s
user 0m0.018s
sys 0m0.003s

Joe "Rotund" Tennies said...

I have 1.7 JDK on a Windows box and I only loaded 361 classes... well only being relative I suppose

Shannon Behrens said...

(Oops, I accidentally deleted a comment I didn't mean to delete:)

Roberto Guerra has left a new comment on your post "Java: Helloooooooo, World!":

Why did they need to import Django's ORM in a shell script? Seems to me like a poorly designed script. Many cases we programmers make poor decisions and write bad code and then blame the language.

Shannon Behrens said...

> Why did they need to import Django's ORM in a shell script? Seems to me like a poorly designed script. Many cases we programmers make poor decisions and write bad code and then blame the language.

It's not uncommon to have to access a database from the command line. Sometimes, you want to use the ORM to do that, in order to make use of the logic the ORM provides.

Sometimes you have to do this to do very complicated database maintenance or migrations. Perhaps you need to write a script that pulls all the user accounts and sends them bulk email. At least in the Ruby on Rails world, I've written multiple scripts that had to make use of the ORM.

Anonymous said...

what about python -S ... I bet it's even faster ;)