Skip to content
This repository has been archived by the owner on Mar 2, 2019. It is now read-only.

Better memory efficient StringUtils join. #22

Merged
merged 2 commits into from
Jul 3, 2015
Merged

Better memory efficient StringUtils join. #22

merged 2 commits into from
Jul 3, 2015

Conversation

monxalo
Copy link
Contributor

@monxalo monxalo commented Jul 1, 2015

Use a StringBuilder instead of the operation '+=' since this would
allocate a new StringBuilder in each Loop iteration.

Changed the parameter type from String to CharSequence to allow
other CharSequence objects like StringBuffers.

Also fixed minor typo in 'delimeter'.

Use a StringBuilder instead of the operation '+=' since this would
allocate a new StringBuilder in each Loop iteration.

Changed the parameter type from String to CharSequence to allow
other CharSequence objects like StringBuffers.

Also fixed minor typo in 'delimeter'.
@alexfu
Copy link
Owner

alexfu commented Jul 3, 2015

Instead of using a Boolean flag to determine if the loop is on the first/last iteration, consider using the classical for loop using just the indices, which is more efficient

@monxalo
Copy link
Contributor Author

monxalo commented Jul 3, 2015

I thought about that too, but according to Item 46 in Effective Java by Joshua Bloch:

The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

// The preferred idiom for iterating over collections and arrays
for (Element e : elements) {
  doSomething(e);
}

When you see the colon (:), read it as “in.” Thus, the loop above reads as “for each element e in elements.” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once.

@alexfu
Copy link
Owner

alexfu commented Jul 3, 2015

The classical for loop eliminates the need for a boolean which isn't needed at all. Granted that the for each shorthand is easier to read and may offer a slight advantage, I would prefer to not have an unnecessary boolean variable.

@monxalo
Copy link
Contributor Author

monxalo commented Jul 3, 2015

Granted that the for each shorthand is easier to read.

That's was also my goal. Although the boolean is necessary for the for each loop, i think it offers a better readability to what happens in each iteration.

But sure we can go for the classical loop.

String result = "";
public static String join(CharSequence delimiter, Object... array) {
StringBuilder sb = new StringBuilder();

for (int i = 0, size = array.length; i < size; i++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For info, the foreach loop dissambled code is exactly this statement for primitive arrays.

alexfu added a commit that referenced this pull request Jul 3, 2015
Better memory efficient StringUtils join.
@alexfu alexfu merged commit fc24976 into alexfu:develop Jul 3, 2015
@monxalo monxalo deleted the patch-string-opt branch July 3, 2015 21:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants