Question

I am running into this weird issue when trying to compare the performance of data type 'int' and 'long', basically I have two unit tests:

@Test
public void testLongOperationPerformance(){
    StopWatch sw = new StopWatch();
    sw.start();
    long count = 0l;
    for(int i = 0; i < Integer.MAX_VALUE; i ++){
        count++;
    }
    sw.stop();
    System.out.println(count);
    System.out.println(sw.elaspedTimeInMilliSeconds());
}

@Test
public void testIntegerOperationPerformance(){
    StopWatch sw = new StopWatch();
    sw.start();
    int count = 0;
    for(int i = 0; i < Integer.MAX_VALUE; i ++){
        count++;
    }
    sw.stop();
    System.out.println(count);
    System.out.println(sw.elaspedTimeInMilliSeconds());
}

These two unit tests are doing the same thing, difference is one use int as the data type for counter and the other uses long for that. The result:

jdk6u32 (64 bit):
test with long
2147483635
96
test with int
2147483647
2

jdk7 (64 bit)
test with long
2147483647
1599
test with int
2147483647
1632

I noticed:

  1. in jdk6u32, test with int is much faster than test with long
  2. in jdk6u32, test results are different between test with int and test with long
  3. in jdk7, both tests are about the same fast and they're both much slower than jdk6u32
  4. in jdk7, both tests got the same result

Can anyone explain why it is like that?

No correct solution

OTHER TIPS

The Java JIT is particularly good at eliminating code which doesn't do anything. In your example, the loop

long count = 0l;
for(int i = 0; i < Integer.MAX_VALUE; i ++){
    count++;
}

can be replaced with

long count = 0l;
count += Integer.MAX_VALUE * 1;

What you are timing is how long it takes to detect and remove the loop. This time can depend on what it has done before so I would suggest testing the loops in a different order to see if that changes the results.

In Java 6 & 7, a number of loop optimisations were optimised incorrectly.

e.g. This infinite loop was not always infinite for some updates

for(int i=0; i < Integer.MAX_VALUE; i += 2)

For this reason, some updates have different optimisations on or off depending on whether they worked for that version. I suggest trying the latest version of Java 7 to see if it makes a difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top