本文介绍了arraylist与数组中原始类型的包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Core java 1中,我已阅读

In "Core java 1" I've read

但是在我的软件中,由于一些要求,我已经使用了Arraylist而不是普通的数组,尽管该软件应该具有高性能,并且在我阅读了引用的文本之后我开始使用恐慌!我可以更改的一件事是将双变量更改为Double以防止自动装箱,我不知道这是否值得,在下一个示例算法中

But in my software I've already used Arraylist instead of normal arrays due to some requirements, though "The software is supposed to have high performance and after I've read the quoted text I started to panic!" one thing I can change is changing double variables to Double so as to prevent auto boxing and I don't know if that is worth it or not, in next sample algorithm

public void multiply(final double val)
    {
        final int rows = getSize1();
        final int cols = getSize2();
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                this.get(i).set(j, this.get(i).get(j) * val);
            }
        }
    }

我的问题是改变双倍双倍有所作为?或者这是微观优化,不会影响任何事情?请记住,我可能正在使用大型矩阵。我应该考虑重新设计整个程序吗?

My question is does changing double to Double makes a difference ? or that's a micro optimizing that won't affect anything ? keep in mind I might be using large matrices.2nd Should I consider redesigning the whole program again ?

推荐答案

double 的重大问题Double 是后者增加了一些内存开销 - 在Sun 32位JVM上每个对象8个字节,可能或多或少在其他JVM上。然后你需要另外4个字节(64位JVM上的8个)来引用该对象。

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.

所以,假设你有1,000,000个对象,差异如下:

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

double[1000000]

每个条目8个字节;总计= 8,000,000字节

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

Double[1000000]

每个对象实例16个字节+每个参考4个字节;总计= 20,000,000字节

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.

这篇关于arraylist与数组中原始类型的包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 19:38