Search code examples
javaperformanceexecutionboxingautoboxing

Performance of Primitive Data types VS their Wrapper class


I tried to measure execution time of primitive data types and their wrapper class for counting same number. I got that wrapper class is taking more time than primitive data types.

Execution time of primitive t1=5 and Execution time of wrapper class t2= 31 in my following code.

import java.io.*;
import java.util.*;
public class Performance
{
  public static long primitive(int count)
     {
   long startTime = System.currentTimeMillis();
   for(int i=0;i<10000;i++)
     count++;
    System.out.println(count);
   long stopTime = System.currentTimeMillis();
   long elapsedTime = stopTime - startTime;
   return elapsedTime;
}
  public static long wrapper(Integer count)
{
    long startTime = System.currentTimeMillis();
    for(int i=0;i<10000;i++)
      count++;
      System.out.println(count);
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    return elapsedTime;
 }

  public static void main(String args[])
  {

   Integer c = new Integer(0);
   long t2=Performance.wrapper(c);
    int count=0;
   long t1=Performance.primitive(count);
  System.out.println("t1="+t1+"t2="+t2);

  }
}

Is there performance difference due to object creation of Wrapper class (Integer) or anything else ?


Solution

  • You are getting essential things wrong here.

    First of all, writing a good micro-benchmark goes way beyond what you are doing in your code; see here for some guidelines.

    Then: you have to understand the things you want to benchmark. For example:

    public static long wrapper(Integer count)
    {
      long startTime = System.currentTimeMillis();
      for(int i=0;i<10000;i++)
        count++
    

    Integer objects are immutable. This code does not only "add" Integer objects, it creates one new Integer object per iteration.

    And of course: your code doesn't even use the computation result. If the JVM/JIT notices that, it might completely throw away that looping and adding construct. So your methods should at least return the final value of count; and the caller should print that result.

    And to answer your specific question: of course using the reference type wrapper classes comes at certain cost. When your program is dealing with (really) high numbers of elements to work on, then using a List of Integer does have considerable higher cost than using an array of int for example. And when you don't pay attention and you are doing implicit autoboxing/unboxing in your code, that can really hurt.

    But the real answer here is: you are focusing on the wrong thing. You see, your first priority should be to do a great design, followed by a well-tested, robust, readable, maintainble implementation. Of course, you should not do outright stupid things and waste performance, but well, before you worry about performance, you better worry about your Java skills in general.

    Long story short: focus on understanding java and on "how do I create a good design"; and forget about "performance" for now. You only have to deal with "performance" when it is a real issue in your application. And then you do real profiling in order to identify the root cause and fix that.