Wrappers of primitive types in arraylist vs arrays

强颜欢笑 提交于 2019-12-04 10:09:12

The big issue with double versus Double is that the latter adds some amount of memory overhead -- 8 bytes per object on a Sun 32-bit JVM, possibly more or less on others. Then you need another 4 bytes (8 on a 64-bit JVM) to refer to the object.

So, assuming that you have 1,000,000 objects, the differences are as follows:

double[1000000]

8 bytes per entry; total = 8,000,000 bytes

Double[1000000]

16 bytes per object instance + 4 bytes per reference; total = 20,000,000 bytes

Whether or not this matters depends very much on your application. Unless you find yourself running out of memory, assume that it doesn't matter.

It changes the place where autoboxing happens, but nothing else.

And 2nd - no, don't worry about this. It is unlikely to be a bottleneck. You can make some benchmarks to measure it for the size of your data, to prove that the difference is insignificant in regard to your application performance.

Double is dramatically more expensive than double, however in 90% of cases it doesn't matter.

If you wanted an efficient matrix class, I would suggest you use one of the libraries which already do this efficiently. e.g. Jama.

Changing the double argument into Double won't help much, it will worsen performance slightly because it needs to be unboxed for the multiplication.

What will help is preventing multiple calls to get() as in:

    for (int i = 0; i < rows; i++)
    {
        List row = this.get(i);

        for (int j = 0; j < cols; j++)
        {
            row.set(j, row.get(j) * val);
        }
    }

(btw, I guessed the type for row.)

Assuming that you use a list of lists, using iterators instead of geting and setting via loop indices will win some more performance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!