One thing we found with string parsing is that if you know your string is purely ASCII it is *much much* faster to grab the string as a byte array, cast each element to a char array and construct the String from that. The charset decoders have a significant overhead.
For example:
byte[] stringBytes = new byte[length];
buffer.get(stringBytes, 0, length);
char[] stringChars = new char[length];
for (int i = 0; i < stringChars.length; i++)
{
stringChars[i] = (char) stringBytes[i];
}
return new String(stringChars);
Although it looks a bit cumbersome it is five times faster (from my measurements) than constructing the String directly from a byte array.
Robert
| "Trustin Lee" <[EMAIL PROTECTED]>
13/04/2006 08:47
|
To: [email protected] cc: Subject: Re: converting ASCII represented longs from ByteBuffer to simple data type? |
On 4/13/06, Michael Bauroth <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> has somebody a short code snippet, how I can convert an ASCII span,
> which represents a long (e.g. 1234) from ByteBuffer to the simple java
> type long most efficiently (minimum of new object generation / buffer
> copies ...)
For now, you have to use ByteBuffer.getString() and then parse it using
Integer.parseInt(). It's not optimal but works fine. :)
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
This communication is for informational purposes only. It is not intended
as an offer or solicitation for the purchase or sale of any financial
instrument or as an official confirmation of any transaction. All market prices,
data and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.
