<< Immutability Of Interfaces | Home | Ruby On Rails: The Video >>

How Many Milliseconds Ago Was That?

Reading a recent article on The Java performance debate over there at OSNews, I can't help but notice passages like the following:

This means that by default a Java application won't overwhelm your system and cause heavy swapping and other such nastiness.

People seem to get so focussed on the milliseconds that they forget that if a task takes 0.01s in C++ and 0.02s in Java, then, "oh no", it's half as slow!

Yet, there is no shortage of Java developers who will tell you that Swing is fast.

The biggest trap Swing programmers fall into is that they don't understand the threading model that is employed.

As a Java developer, I will happily sacrifice some memory, and a bit of speed, because ...

In an article aimed at dispelling the dual myth of "Java is slow/Java is a memory hog," they sounded more like excuses than good arguments and advises.

And I contend that this "Can't win, Won't try" attitute is exactly what hurts Java programs and programmers the most. You see, when you subconsciously say to yourself "I'm writing in Java because I want to trade some of that performance off for programming convenience," you are less likely to pay attention to the little things that might have a negative performance impact. And these little things accumulate faster than you think. A dozen milliseconds here, a dozen milliseconds there, pretty soon you are talking about hundreds of milliseconds. And that might be bad for the whole program.

And that brings me to the problem that's bothering me since last week. I wanted measure as accurately as possible the "start up time" of my JVM, when running the "Hello World" program—the elapsed time between the moment the shell forks the Java process and the moment control pass into the main() method.

Any ideas?



Re: How Many Milliseconds Ago Was That?

I think this will do it: public class time { public static void main(String[] args) { System.out.println(System.nanoTime()); } } date +%N; java -cp /tmp time | cut -c 11-; date +%N; date +%N; java -cp /tmp time | cut -c 11-; java -cp /tmp time | cut -c 11- You might accidentally hit it when it is rolling over a second, but you should be able to get output that looks like this: 085422000 134805000 181921000 183884000 232776000 326522000 The difference between the first and second one is the amount of time to finish running the date command and start the java process. The difference between the second one and the third one is how long it takes to shutdown the java process and start the date process. The difference between the third and fourth is how long it takes to shutdown and start the date process. All these times are in ns. Converting to ms we have: End date + Start Java = 48ms End Java + Start date = 47ms End date + Start date = 2ms Start java + End java = 93ms Now we have a system of linear equations that we can solve to find that it takes 46.5ms to start java and 46.5ms to end it. This was all performed using Java 1.5 from Sun on a CentOS 4.0 Linux box with a 3.0 ghz P4.

Re: How Many Milliseconds Ago Was That?

Thanks for the hint. I's can also use "date +%s%N" so that I don't have to worry about second roll over. However this doesn't seem to work on Windows as the outputs of System.nanoTime() and date +%s%N do not seem to be related.

The there is the problem with the system of linear equations. The equations are not all independent and cannot be solved without an additional simplifying assumption. "End date = 0" seems to be a reasonable assumption

Re: How Many Milliseconds Ago Was That?

Oh boy, another performance debate! Programmers like to debate performance almost as much as we like to debate 2 versus 4 space indentation...don't even get me started on tabs. My advice? Write maintainable code, avoid duplication, write unit tests...then measure performance using a profiler. Correct problems when they are observed.

Re: How Many Milliseconds Ago Was That?

Of course 2 spaces is all you need, anything more than that is a waste of screen real estate. :)

Seriously, I'm merely pointing out that in order to write performant code, you have to i) care about it, and ii) measure it. It also helps to know the cost of various programming constructs.

And I'm just not comfortable that I don't even know how to accurately measure the start up time of a hello world program.

Re: How Many Milliseconds Ago Was That?

Why not use the UNIX time command? It can tell you a lot about the process, including page faults and memory usage. It also reports CPU time used, so other processes grabbing the CPU won't effect the results. The only problem is that it only has 1/100 of a second accuracy. You could easily run a for loop to run your test program multiple times to get around that though. (You'd have to use a script; time isn't built in to bash.)

Re: How Many Milliseconds Ago Was That?

You should run Gentoo, you ricer.

Add a comment Send a TrackBack