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.

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:

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.

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

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