Go vs Java.

Reading from file and counting words using a map.

Alin Velican
2 min readNov 23, 2019
Photo by Ryan Quintal on Unsplash

I assume that everyone know what Java is. Go is becoming more and more popular, is the de facto cloud language and this is happening because it has a lot of advantages. One of them is performance, due to the fact that it is a compiled language. Let’s find out what that really means in numbers!

What I measured

For this benchmark I used a simple program which read from a big text file and count the number of occurrences for every word using a map. This is the code for Java. The text file has something like 4.5 millions lines which means 182MB.This is the Java code:

This is the Go code:

And results For Java I get this:

For Go:

Comments

So the Go is 2 time faster than Java echivalent, and also has a more elegant solution.

Some improvements

For Java variant I made some improvements in code in order to get a better performance, and I don’t use anymore split() function. In place of it I use substring(). This is the code:

For this I get better results:

Comments

Compared with Go version it is still ~50% slower, but not bad.

Memory usage:

Another important aspect related to performance is memory consumption. For this I used the /usr/bin/time -l from MacOS. Let’s see the results:

For Java I get over 1GB maximum memory usage.

For Go I get 7MB maximum memory usage

Looking through this perspective, Go seems a far better choice.

--

--