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


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

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

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p