简体   繁体   中英

Array of byte or ByteBuffer, what is faster

I'm looking for the best way to do in my application, and now I'm wondering:

I received a byte array (byte[]) by reading an InputStream. Then I have to do some treatment, so I pass this array as a parameter of a method. Coming from C++ language, and as I have to do this treatment as fast as possible, and many time, I am wondering if passing a byte array by value might not cause problems.

Is it worth creating a ByteBuffer object to pass a reference to my method instead of a value?

Thank you for your help and advices.

In Java arrays are objects, so you are actually passing a copy of the reference variable to this object.

IIRC in C++ passing an array is the equivalent of passing a pointer to the first element.

Passing a byte[] reference is just like passing an ordinal object reference. There will be no copying of the array. It is not worth to wrap around with a ByteBuffer .

Generaly, working with primitive types is faster as it requires less memory to store the data.

I would therefore suggest to keep the byte array solution. This, of course depends what "treatment" you intend to do with it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM