Bidvertiser

Friday, April 5, 2013

Comparison of primitive type vs Wrapper object

After coming across this question in several interviews, came up with a small piece of code to compare primitive type and wrapper object.  Auto boxing after Java 5 has shadowed the behind seen happenings to fellow developers. :-)

There will be n number of pages in google to give the differences, I am just giving a small program to compare the performance of int and Integer.


import java.util.Date;

/**
 * PerformanceTester.java
 *
 * @author cananth
 */
public class PerformanceTester {
   public void processWithWrapper() {
      long startTime = new Date().getTime();
      Integer ctr = 0;
      for (int i = 0; i < Integer.MAX_VALUE; i++) {
         ctr += i;
      }
      long endtime = new Date().getTime();
      System.out.println("---------------------------------------------------");
      System.out.println("Time using Integer: " + (endtime - startTime) + " ms");
      System.out.println("---------------------------------------------------");
   }

   public void processWithPrimitive() {
       long startTime = new Date().getTime();
       int i = 0;
       int ctr = 0;
       for (i = 0; i < Integer.MAX_VALUE; i++) {
           ctr += i;
       }
       long endtime = new Date().getTime();
       System.out.println("Time using int: " + (endtime - startTime) + " ms");
       System.out.println("---------------------------------------------------");
   }

   public static void main(String[] args) {
       System.out.println("Same Object");
       System.out.println("---------------------------------------------------");
       PerformanceTester test = new PerformanceTester();
       test.processWithWrapper();
       test.processWithPrimitive();
       test.processWithWrapper();
       test.processWithPrimitive();
             
       System.out.println();
       System.out.println("New Object");
       System.out.println("---------------------------------------------------");
       new PerformanceTester().processWithWrapper();
       new PerformanceTester().processWithPrimitive();
       new PerformanceTester().processWithWrapper();
       new PerformanceTester().processWithPrimitive();
   }
}

Output:
Same Object
---------------------------------------------------
Time using Integer: 7835 ms
---------------------------------------------------
Time using int: 1505 ms
---------------------------------------------------
Time using Integer: 7685 ms
---------------------------------------------------
Time using int: 1489 ms
---------------------------------------------------

New Object
---------------------------------------------------
Time using Integer: 7369 ms
---------------------------------------------------
Time using int: 1 ms
---------------------------------------------------
Time using Integer: 7356 ms
---------------------------------------------------
Time using int: 1 ms
---------------------------------------------------

The difference in execution is huge.  In few of my trials I have got 0ms too for the int.

Verdict: Never use wrapper class for computations.

submit to reddit

1 comment:

  1. Great :)

    Another article about primitive and wrapper

    http://www.behindjava.com/2014/06/primitive-and-wrapper-classes-in-java.html

    ReplyDelete