Posts Tagged ‘patterns’

Missing MethodTimer.computeTotalTime() in Implementation Patterns

Thursday, March 13, 2008

While preparing a review of Kent Beck’s Implementation Patterns book for stacktrace.it I stumbled upon a missing detail in Appendix A (Performance Measurement): where is the implementation of MethodTimer.computeTotalTime()?

I didn’t pay much attention while reading the book: Appendix A is one of my favourite chapters because you can finally see what Beck means in practice, but I haven’t really considered all the implementation details, only the overall philosophy. For the review I decided to go deeper and I couldn’t really understand what happened to MethodTimer.computeTotalTime(). In addition, until you realize how that method should look like, maybe you find yourself in troubles trying to fully grasp the design. I certainly did. Google didn’t help (I guess that Beck for whatever reason didn’t want to publish the code), so I did it by myself.

 private long computeTotalTime() throws Exception {
 	long start = System.nanoTime();
 	for (int i = 0; i < iterations; i++)
 		method.invoke(instance, new Object[0]);
 	return System.nanoTime() - start;
 }

This is not rocket science of course, but I had a few false starts before getting it (also because I made a few mistakes copying the code from the book and I was misled by the results). Hope it can help you as well.