Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traversal performance of multidimensional array in Java

In code and the results below, We can see that “Traverse2” is much faster than "Traverse1", indeed they just traverse the same number of elements.

1.How does this difference happened?

2.Putting longer interation inside shorter interation will have a better performance?

public class TraverseTest {

    public static void main(String[] args)
    {
        int a[][] = new int[100][10];
        System.out.println(System.currentTimeMillis());

        //Traverse1
        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < 10; j++)
                a[i][j] = 1;
        }

        System.out.println(System.currentTimeMillis());

        //Traverse2
        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < 100; j++)
                a[j][i] = 2;
        }

        System.out.println(System.currentTimeMillis());
    }
}

Result:

1347116569345

1347116569360

1347116569360

If i change it to

System.out.println(System.nanoTime());

The result will be:

4888285195629

4888285846760

4888285914219

It means that if we put longer interation inside will have a better performance. And it seems to have some conflicts with cache hits theory.

like image 261
Jingjing Zhong Avatar asked Jul 14 '26 23:07

Jingjing Zhong


2 Answers

I suspect that any strangeness in the results you are seeing in this micro-benchmark are due to flaws in the benchmark itself.

For example:

  • Your benchmark does not take account of "JVM warmup" effects, such as the fact that the JIT compiler does not compile to native code immediately. (This only happens after the code has executed for a bit, and the JVM has measured some usage numbers to aid optimization.) The correct way to deal with this is to put the whole lot inside a loop that runs a few times, and discard any initial sets of times that that look "odd" ... due to warmup effects.

  • The loops in your benchmark could in theory be optimized away. The JIT compiler might be able to deduce that they don't do any work that affects the program's output.

Finally, I'd just like to remind you that hand-optimizing like this is usually a bad idea ... unless you've got convincing evidence that it is worth your while hand-optimizing AND that this code is really where the application is spending significant time.

like image 172
Stephen C Avatar answered Jul 16 '26 12:07

Stephen C


First, always run microbenchmark tests several times in a loop. Then you'll see both times are 0, as the array sizes are too small. To get non-zero times, increase array sizes in 100 times. My times are roughly 32 ms for Traverse1 and 250 for Traverse2. The difference is because processor use cache memory. Access to sequential memory addresses is much faster.

like image 32
Alexei Kaigorodov Avatar answered Jul 16 '26 12:07

Alexei Kaigorodov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!