Skip to main content

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"? ;-)


Comments

rog said…
On my machine, Go: 0.007s

Go has no class either :-)
Anonymous 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.
jjinux 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
I have 1.7 JDK on a Windows box and I only loaded 361 classes... well only being relative I suppose
jjinux 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.
jjinux 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 ;)

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from