ArrayList <Byte> vs String in Java

I am implementing the LZW algorithm. I have successfully implemented it for strings and text files and am currently modifying my code to work with binary files such as images or executables (since I cannot read these files as strings).

I replaced the type Stringin my code with the type ArrayList<Byte>. My code now compresses and decompresses binaries correctly, however it is at least 10 times slower! This is not acceptable in a compression application where speed is a critical element.

I made the right replacement ArrayList<Byte>for String. Is there a faster alternative with similar functionality? Please note that the LZW algorithm requires resizing the array, so the standard ones are arrays[]not suitable.

Sincerely.

+4
source share
4 answers

Usage List<Byte>will put each byte in a separate instance of the object.
In bulk, this is one of the worst things you can do for performance.

In contrast, an array or string can occupy a solid block of memory.

Instead, you should use ByteArrayOutputStreamor use byte[]directly and resize as needed (you can make a wrapper class for this)

+6
source

boxing byte ArrayList, , String s. , byte . , 4-8 , !

byte [] , , ( ), this this.

+1
0
source

ArrayListimplements an array, so it is not ideal for large size resizing. LinkedListshould provide better performance if resizing creates a bottleneck.

fooobar.com/questions/32 / ...

0
source

All Articles