Coder Social home page Coder Social logo

boxing-unboxing-java-perf's Introduction

Box / Unboxing vs. Encapsulation: Java's Map<Something, Integer>

Problem: I want a map that store integer as its value. Now i have two approaches:

  1. Use anything as key and Integer (non-primitive integer) as value, or
  2. Use anything as key and a class that contains int (primitive integer) as value.

Which should i use? The first solution seems pretty neat, nothing to do. Just instatiate new Integer and done. At the other hand, performance wise, it will do unnecessary boxing/unboxing if not properly handled. Remember that we only able to use primitive integer to do calculation and primitive integer to store value to the map.

The Test

Benchmark withBoxing = new Benchmark() {
  public void run() {
    Map<String, Integer> a = new HashMap<String, Integer>();

    // Simulate much boxing
    for (int i = 0; i < MAX; i++) {
      a.put("" + i, i);
    }

    // Simulate much unboxing
    for (int i = 0; i < MAX; i++) {
      // A hack so that JVM does not optimize this code.
      hack += a.get("" + i); 
      hack = hack % 1000;
    }
  }
};
Benchmark encapsulate = new Benchmark() {
  public void run() {
    Map<String, EncapsulatedInteger> a = new HashMap<String, EncapsulatedInteger>();

    // No boxing/unboxing, but new class
    for (int i = 0; i < MAX; i++) {
      a.put("" + i, new EncapsulatedInteger(i));
    }

    for (int i = 0; i < MAX; i++) {
      // A hack so that JVM does not optimize this code.
      hack += a.get("" + i).v; 
      hack = hack % 1000;
    }
  }
};

The run command

javac MapPerf.java; java -Xmx1024m -cp . MapPerf

The Result

$ javac MapPerf.java; java -Xmx1024m -cp . MapPerf
Boxing/unboxing: 10973 ms
Encapsulation: 7036 ms

Summary

With Integer as value, the routine is 50% slower because of unnecessary boxing/unboxing

boxing-unboxing-java-perf's People

Contributors

mufid avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.