Most efficient way to concatenate Strings

Derek Noble

I was under the impression that StringBuffer is the fastest way to concatenate strings, but I saw this Stack Overflow post saying that concat() is the fastest method. I tried the 2 given examples in Java 1.5, 1.6 and 1.7 but I never got the results they did. My results are almost identical to this

  1. Can somebody explain what I don't understand here? What is truly the fastest way to concatenate strings in Java?

  2. Is there a different answer when one seeks the fastest way to concatenate two strings and when concatenating multiple strings?

Matt Timmermans

String.concat is faster than the + operator if you are concatenating two strings... Although this can be fixed at any time and may even have been fixed in java 8 as far as I know.

The thing you missed in the first post you referenced is that the author is concatenating exactly two strings, and the fast methods are the ones where the size of the new character array is calculated in advance as str1.length() + str2.length(), so the underlying character array only needs to be allocated once.

Using StringBuilder() without specifying the final size, which is also how + works internally, will often need to do more allocations and copying of the underlying array.

If you need to concatenate a bunch of strings together, then you should use a StringBuilder. If it's practical, then precompute the final size so that the underlying array only needs to be allocated once.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there an efficient way to concatenate strings

From Dev

Most Efficient Method to Concatenate Strings in Python

From Dev

Most efficient way of comparing long arrays of strings

From Dev

Most efficient way to concat strings in javascript

From Dev

Most efficient way to remove substrings from an array of strings

From Dev

What is the most efficient way to implement mutable strings in C?

From Dev

Most efficient way to implement this?

From Dev

Most efficient way to execute this

From Java

Most efficient way to concatenate words from a big CSV file: pandas or Python standard library?

From Dev

Most efficient way to compute a polynomial

From Dev

query with calculations most efficient way

From Dev

Most efficient way to split sentence

From Dev

Most efficient way to rename an image

From Dev

Most efficient way to output a newline

From Dev

Most efficient way to loop through '...'

From Dev

Most efficient way to determine an intersection

From Dev

Most efficient way to count occurrences?

From Dev

Most efficient way to compute a polynomial

From Dev

Most efficient way to store this data

From Dev

Is this the most efficient way to write this method?

From Dev

Most efficient way of MySQL rows?

From Dev

Most efficient way to encrypt files?

From Dev

query with calculations most efficient way

From Dev

Most efficient way to search in a table

From Dev

Most efficient way to write a buffer

From Dev

Most efficient way to look for these inconsistencies?

From Dev

Most efficient way to bin a pandas series of date strings by day/month/year?

From Dev

Most time efficient way to remove stop words in Java from an array of strings

From Dev

What's the most efficient way to get frequency of occurrence of strings in a LARGE list?

Related Related

  1. 1

    Is there an efficient way to concatenate strings

  2. 2

    Most Efficient Method to Concatenate Strings in Python

  3. 3

    Most efficient way of comparing long arrays of strings

  4. 4

    Most efficient way to concat strings in javascript

  5. 5

    Most efficient way to remove substrings from an array of strings

  6. 6

    What is the most efficient way to implement mutable strings in C?

  7. 7

    Most efficient way to implement this?

  8. 8

    Most efficient way to execute this

  9. 9

    Most efficient way to concatenate words from a big CSV file: pandas or Python standard library?

  10. 10

    Most efficient way to compute a polynomial

  11. 11

    query with calculations most efficient way

  12. 12

    Most efficient way to split sentence

  13. 13

    Most efficient way to rename an image

  14. 14

    Most efficient way to output a newline

  15. 15

    Most efficient way to loop through '...'

  16. 16

    Most efficient way to determine an intersection

  17. 17

    Most efficient way to count occurrences?

  18. 18

    Most efficient way to compute a polynomial

  19. 19

    Most efficient way to store this data

  20. 20

    Is this the most efficient way to write this method?

  21. 21

    Most efficient way of MySQL rows?

  22. 22

    Most efficient way to encrypt files?

  23. 23

    query with calculations most efficient way

  24. 24

    Most efficient way to search in a table

  25. 25

    Most efficient way to write a buffer

  26. 26

    Most efficient way to look for these inconsistencies?

  27. 27

    Most efficient way to bin a pandas series of date strings by day/month/year?

  28. 28

    Most time efficient way to remove stop words in Java from an array of strings

  29. 29

    What's the most efficient way to get frequency of occurrence of strings in a LARGE list?

HotTag

Archive