Good afternoon all, I'm using a java.lang.StringBuilder to store some characters. I have no idea how many characters I'm going to store in advance, except that:
How do we go about calculating the optimal initial buffer length that should be used?
Currently I'm using new java.lang.StringBuilder(4000)
but that's just because I was too lazy to think previously.
Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
By default, an empty StringBuffer contains 16 character capacity.
Java StringBuffer capacity() method The capacity refers to the total amount of characters storage size in string buffer. An empty StringBuffer class contains the default 16 character capacity.
To find the length of the StringBuffer object, the length() function is used. It is a method of the StringBuffer Class. The method returns the count of characters in the sequence.
There are two factors here: time and memory consumption. The time is mostly influenced by the number of times java.lang.AbstractStringBuilder.expandCapacity()
is called. Of course the cost of each call is linear to the current size of the buffer, but I am simplifying here and just counting them:
expandCapacity()
(time)StringBuilder
will expand 0 timesStringBuilder
will expand 8 timesStringBuilder
will expand 11 timesThe expected number of expandCapacity
is 3,23.
StringBuilder
will expand 0 timesStringBuilder
will expand 3 timesThe expected number of expandCapacity
is 0,03.
As you can see the second scenario seems much faster, as it very rarely has to expand the StringBuilder
(three time per every 100 inputs). Note however that first expands are less significant (copying small amount of memory); also if you add strings to the builder in huge chunks, it will expand more eagerly in less iterations.
On the other hand the memory consumption grows:
StringBuilder
will occupy 16 charactersStringBuilder
will occupy 4K charactersStringBuilder
will occupy 32K charactersThe expected average memory consumption is: 1935 characters.
StringBuilder
will occupy 4K charactersStringBuilder
will occupy 32K charactersThe expected average memory consumption is: 4383 characters.
This makes me believe that enlarging the initial buffer to 4K will increase the memory consumption by more than two times while speeding up the program by two orders of magnitude.
The bottom line is: try! It is not that hard to write a benchmark that will process million strings of various length with different initial capacity. But I believe a bigger buffer might be a good choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With