ArrayList的&LT;&字节GT; VS在Java的String [英] ArrayList<Byte> vs String in Java
问题描述
我实现LZW算法。我已经实现了它成功的字符串和文本文件和我目前正在修改我的code二进制文件,如图像或可执行文件(因为我无法阅读这些文件作为字符串)工作。
I am implementing the LZW algorithm. I have implemented it for strings and text files successfully and am currently modifying my code to work with binary files, such as images or executables (since I cannot read these files as strings).
我已经取代了字符串
键入我的code与的ArrayList&LT;字节&GT;
键入。我的code现在COM presses和DECOM $ P $正确psses二进制文件,但它至少是慢10倍!这是不能接受的com pression应用程序,其中速度是一个关键因素。
I have replaced the String
type in my code with the ArrayList<Byte>
type. My code now compresses and decompresses binary files correctly, however it is at least 10x slower! This is unacceptable in a compression application where speed is a critical element.
我讲的正确替代的ArrayList&LT;&字节GT;
为字符串
。是否有类似功能的更快的替代方案?请注意,LZW算法需要调整大小的数组标准,因此数组[]
是不适合的。
Have I made the correct substitution of ArrayList<Byte>
for String
. Is there a faster alternative with similar functionality? Note that the LZW algorithm requires array resizing hence standard arrays[]
are not suitable.
问候。
推荐答案
使用列表&LT;字节方式&gt;
将框的每一个字节到一个单独的对象实例结果
散装,那是最糟糕的事情你可以做的性能之一。
Using a List<Byte>
will box every byte into a separate object instance.
In bulk, that is one of the worst possible things you can do for performance.
相反,一个数组或字符串可以占用内存的固体块。
By contrast, an array or string can occupy a solid block of memory.
相反,你应该使用 ByteArrayOutputStream
或字节[]
直接使用,并根据需要调整(可以使对于包装类)
Instead, you should use ByteArrayOutputStream
, or use byte[]
directly and resize as needed (you can make a wrapper class for that)
这篇关于ArrayList的&LT;&字节GT; VS在Java的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!