列表< Double>那个使用了双[]的RAM? [英] List<Double> that uses RAM of double[]?

查看:117
本文介绍了列表< Double>那个使用了双[]的RAM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java专家强调避免过早优化的重要性,而将重点放在干净的OO设计上。我试图在重写使用大量长元素(几百万)的程序的情况下调和这一原则。看起来使用ArrayList会耗费大量3x的长时间的原始数组的内存,浪费了大量的RAM似乎是一个合理的关注点。



我基于这个实验我使用了MemoryTestBench类在这里描述的。我的测试和输出如下:

 包内存; 

import java.util.ArrayList;
import java.util.List;

public class ArrayListExperiment {

public static void main(String [] args){

ObjectFactory arrayList = new ObjectFactory(){
public Object makeObject(){
列表< Long> temp = new ArrayList< Long>(1000);
for(long i = 0; i< 1000; i ++)
temp.add(i);
return temp;
}
};

ObjectFactory primitiveArray = new ObjectFactory(){
public Object makeObject(){
long [] temp = new long [1000]; (int i = 0; i <1000; i ++)
temp [i] = i;
return temp;
}
};

MemoryTestBench memoryTester = new MemoryTestBench();
memoryTester.showMemoryUsage(primitiveArray);
memoryTester.showMemoryUsage(arrayList);
}
}

并输出:

  memory.ArrayListExperiment $ 2生成[J,取8016字节
memory.ArrayListExperiment $ 1生成java.util.ArrayList,取24968字节

我的问题是:如何获得OO列表的优点,仍然保留原始数组的小内存占用?我认为番石榴可能会提供答案,但是通过API来看,我使用哪个类代替ArrayList并不明显。



感谢任何建议。 >

解决方案

您可以考虑使用 Trove ,它为原始集合提供支持,例如 TDoubleArrayList 类:


可调整大小的数组 - 支持双基元列表。


编辑:这个类没有实现列表,但这是Java避免使用盒装图元的价格。 Guava的解决方案是最通用的,而Trove最适合于更加极端的性能要求。


Java experts emphasize the importance of avoiding premature optimization, and focusing instead on clean OO design. I am trying to reconcile this principle in the context of rewriting a program that uses a large array of long elements (a few million). It seems that using an ArrayList would consume about 3x the memory of a primitive array of longs, and wasting that much RAM seems like a legitimate concern to me.

I am basing this off an experiment I did using MemoryTestBench class described here. My test and output are as follows:

package memory;

import java.util.ArrayList;
import java.util.List;

public class ArrayListExperiment {

public static void main(String[] args) {

    ObjectFactory arrayList = new ObjectFactory() {
        public Object makeObject() {
            List<Long> temp = new ArrayList<Long>(1000);
            for (long i=0; i<1000; i++)
                temp.add(i);
            return temp;
        }
    };

    ObjectFactory primitiveArray = new ObjectFactory() {
        public Object makeObject() {
            long[] temp = new long[1000];
            for (int i=0; i<1000; i++)
                temp[i] = i;
            return temp;
        }
    };

    MemoryTestBench memoryTester = new MemoryTestBench();
    memoryTester.showMemoryUsage(primitiveArray);
    memoryTester.showMemoryUsage(arrayList);
}
}

and output:

memory.ArrayListExperiment$2 produced [J which took 8016 bytes
memory.ArrayListExperiment$1 produced java.util.ArrayList which took 24968 bytes

My question is: How can I reap the benefits of an OO List and still retain the small memory footprint of a primitive array? I think guava might provide the answer, but glancing through the API it's not obvious to me which class to use in place of ArrayList.

Thanks for any suggestions.

解决方案

You might consider using Trove, which provides support for primitive collections, for example the TDoubleArrayList class:

A resizable, array-backed list of double primitives.

Edit: It's true that this class doesn't implement List, but that's Java's price of avoiding boxed primitives. Guava's solution is the most versatile, while Trove is best for more extreme performance requirements.

这篇关于列表&lt; Double&gt;那个使用了双[]的RAM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆