CharBuffer vs. StringBuffer

Performance of java.nio.CharBuffer vs. java.lang.StringBuffer:

 Iterations 

 CharBuffer 

 StringBuffer 

  % diff 

10000

~0

~0

0

1000000

187

243

77

For “normal” string processing there appears to be no difference between the two — the effects are lost in the noise. For large strings (documents and the like), CharBuffer has a distinct advantage.

CharBuffer has the perk of pointer-like manipulation via CharBuffer.subSequence() and CharBuffer.slice() but lacks good string searching functions like StringBuffer.lasIndexOf().

The only caveat with CharBuffer is that the size of the buffer must be known a priori.

Notes:

  • The length of the strings added were between 0 and 10 characters in length to simulate standard text processing.
  • This should be taken with a grain of salt since it is a micro-benchmark.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s