The most efficient way to populate a string with a specified length with a specified character?

Basically, given int, I need to generate a String with the same length containing only the specified character. A related question is here , but it relates to C #, and it matters what's in the line.

This question , and my answer to this is why I ask about it. I'm not sure the best way to do this is in terms of performance.

Example

Method Signature:

String getPattern(int length, char character); 

Using:

 //returns "zzzzzz" getPattern(6, 'z'); 

What i tried

 String getPattern(int length, char character) { String result = ""; for (int i = 0; i < length; i++) { result += character; } return result; } 

Is this the best I can do in terms of performance?

0
source share
7 answers

You should use StringBuilder instead of concatenating characters this way. Use StringBuilder.append () .

StringBuilder will give you better performance. The problem with concatenation is that every time a new line is created (the line is immutable), the old line is copied, a new line is added, and the old line is deleted. This is a large part of the extra work that during a period of type (for example, in a large cycle) will lead to poor performance.

+6
source

Commons-lang StringUtils or Guava Strings are your friends. As already mentioned, avoid string concatenations.

 StringUtils.repeat("a", 3) // => "aaa" Strings.repeat("hey", 3) // => "heyheyhey" 
+5
source

Use primitive char arrays and some standard usage classes like Arrays

 public class Test { static String getPattern(int length, char character) { char[] cArray = new char[length]; Arrays.fill(cArray, character); // return Arrays.toString(cArray); return new String(cArray); } static String buildPattern(int length, char character) { StringBuilder sb= new StringBuilder(length); for (int i = 0; i < length; i++) { sb.append(character); } return sb.toString(); } public static void main(String args[]){ long time = System.currentTimeMillis(); getPattern(10000000,'c'); time = System.currentTimeMillis() - time; System.out.println(time); //prints 93 time = System.currentTimeMillis(); buildPattern(10000000,'c'); time = System.currentTimeMillis() - time; System.out.println(time); //prints 188 } 

}

EDIT Arrays.toString () gave lower performance since it ended up using StringBuilder, but the new String did the magic.

+4
source

Yikes, no.

A String is immutable in java; you cannot change it. When you speak:

 result += character; 

Each time create a new String .

You want to use StringBuilder and add to it, and then return String using the toString() method.

+3
source

I think it would be more efficient to do this, as shown below,

 String getPattern(int length, char character) { char[] list = new char[length]; for(int i =0;i<length;i++) { list[i] = character; } return new string(list); } 
+2
source

String concatenation is never the most efficient since String is immutable, for best performance you should use StringBuilder and append ()

 String getPattern(int length, char character) { StringBuilder sb= new StringBuilder(length) for (int i = 0; i < length; i++) { sb.append(character); } return sb.toString(); } 
+1
source

In my opinion, you will have better results creating a little String and concatenation (using StringBuilder , of course) until you reach the request size: concatenating / adding "zzz" to "zzz" is probably more than concatenating "z" three times (well, maybe not for such small numbers, but when you reach 100 or so characters, doing ten "z" concatenations followed by ten "zzzzzzzzzz" concatenations is probably better than 100 'g' concatenates).

Also, since you are asking about GWT, the results will vary greatly between DevMode (pure Java) and "production mode" (executed in JS in the browser) and will probably vary depending on the browser.

The only way to really know is a benchmark, everything else is pure speculation.
And perhaps use delayed binding to use the most efficient option in every browser (which is exactly how StringBuilder emulated in GWT).

+1
source

Source: https://habr.com/ru/post/896035/


All Articles