Micro-benchmarking Java Code With Caliper

Benchmarking Java code can be tricky, as the Java's JIT compiler optimizes the code for high-speed execution. But there are cases where you can test Java code for performance statistics. Caliper is one such tool, which can be used to benchmark java code. More precisely, it is a Micro-Benchmarking tool, used only for testing the performance of small units of Java code.

To test how Caliper works, I worked on a sample Maven based project. The code that I wanted to test is to check if a Java's HashMap contains a key and get it, if the key exists. The usual way of doing this is:
if(someMap.containsKey(key)) {
      someValue = someMap.get(key);
}
But, the below code performs better:
someValue = someMap.get(key);
if(someValue != null) {}
Although 'containsKey' operation is the same as 'get', the above code performs better, as containsKey operation does some additional work. To micro-benchmark this code, I've worked on a simple project and the sources are listed below: Below is the pom.xml dependency for Caliper:


<dependency>
    <groupId>com.google.caliper</groupId>
    <artifactId>caliper</artifactId>
    <version>0.5-rc1</version>
</dependency>

And below is the code for the Caliper Demo class file:



Below are the results for the above test (displaying both 'linear' and 'logarithmic' run-times):





After running the above test, I ran the same code with different length's, by changing the @Param values to "@Param({"50", "100", "1000", "10000"})" and the results are as below (displaying both 'linear' and 'logarithmic' times):





PS: Premature Optimization can still be the root of all evils. :P

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.