Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Minor visual improvements for better readability.
Source Link
informatik01
  • 16.5k
  • 11
  • 82
  • 112

In most cases, you won't see an actual difference between the two approaches, but it's easy to construct a worst case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for (int i=0;i<100000;i++i = 0; i < 100_000; i++) s.append("*"); } private static void slow() { String s = ""; for (int i=0;i<100000;i++i = 0; i < 100_000; i++) s+="*";s += "*"; } } 

The output is:

slow elapsed 11741 ms fast elapsed 7 ms 

The problem is that to +=+= append to a string reconstructsreconstructs a new string, so it costs something linearlinear to the length of your strings (sum of both).

 

So - to your question:

The second approach would be faster, but it's less readable and harder to maintain. As I said, in your specific case you would probably not see the difference.

In most cases, you won't see an actual difference between the two approaches, but it's easy to construct a worst case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

The output is:

slow elapsed 11741 ms fast elapsed 7 ms 

The problem is that to += append to a string reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

So - to your question:

The second approach would be faster, but it's less readable and harder to maintain. As I said, in your specific case you would probably not see the difference.

In most cases, you won't see an actual difference between the two approaches, but it's easy to construct a worst case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for (int i = 0; i < 100_000; i++) s.append("*"); } private static void slow() { String s = ""; for (int i = 0; i < 100_000; i++) s += "*"; } } 

The output is:

slow elapsed 11741 ms fast elapsed 7 ms 

The problem is that to += append to a string reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

 

So - to your question:

The second approach would be faster, but it's less readable and harder to maintain. As I said, in your specific case you would probably not see the difference.

Minor edit: grammar/spelling/case/punctation/etc.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

inIn most cases, you wontwon't see an actual difference between the two approaches, but it's easy to construct a worseworst case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

The output is:

slow elapsed 11741 ms fast elapsed 7 ms 

theThe problem is that to += append to a string reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

soSo - to your question: the

The second approach would be faster, but it's less readable and harder to maintain. asAs I said, in your specific case you would probably not see the difference.

in most cases, you wont see actual difference between the two approaches, but it's easy to construct a worse case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

output is:

slow elapsed 11741 ms fast elapsed 7 ms 

the problem is that to += append to a string reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

so - to your question: the second approach would be faster, but it's less readable and harder to maintain. as I said, in your specific case you would probably not see the difference.

In most cases, you won't see an actual difference between the two approaches, but it's easy to construct a worst case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

The output is:

slow elapsed 11741 ms fast elapsed 7 ms 

The problem is that to += append to a string reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

So - to your question:

The second approach would be faster, but it's less readable and harder to maintain. As I said, in your specific case you would probably not see the difference.

added 64 characters in body
Source Link
Omry Yadan
  • 34.1k
  • 19
  • 72
  • 89

in most cases, you wont see actual difference between the two approaches, but it's easy to construct a worse case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

output is:

slow elapsed 11741 ms fast elapsed 7 ms 

the problem is that to += append to a string, java need to reconstruct reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

so - to your question: the second approach would be faster, but it's less readable and harder to maintain. as I said, in your specific case you would probably not see the difference.

in most cases, you wont see actual difference between the two approaches, but it's easy to construct a worse case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

output is:

slow elapsed 11741 ms fast elapsed 7 ms 

the problem is that to append to a string, java need to reconstruct a new string.

so - to your question: the second approach would be faster, but it's less readable and harder to maintain. as I said, in your specific case you would probably not see the difference.

in most cases, you wont see actual difference between the two approaches, but it's easy to construct a worse case scenario like this one:

public class Main { public static void main(String[] args) { long now = System.currentTimeMillis(); slow(); System.out.println("slow elapsed " + (System.currentTimeMillis() - now) + " ms"); now = System.currentTimeMillis(); fast(); System.out.println("fast elapsed " + (System.currentTimeMillis() - now) + " ms"); } private static void fast() { StringBuilder s = new StringBuilder(); for(int i=0;i<100000;i++) s.append("*"); } private static void slow() { String s = ""; for(int i=0;i<100000;i++) s+="*"; } } 

output is:

slow elapsed 11741 ms fast elapsed 7 ms 

the problem is that to += append to a string reconstructs a new string, so it costs something linear to the length of your strings (sum of both).

so - to your question: the second approach would be faster, but it's less readable and harder to maintain. as I said, in your specific case you would probably not see the difference.

Source Link
Omry Yadan
  • 34.1k
  • 19
  • 72
  • 89
Loading