GWT + string concatenation operator against stringbuffer

I had to choose a way to efficiently concatenate string strings for a GWT application. To do this, I did a little test and thought it would be useful for others to know the results.

Thus, the amazing difference is pretty slight: ~ 100 ms for 1,000,000 concatenations. Therefore, please choose the appropriate code in terms of reading the code.

My testing was simple:

// + operator private void str() { Date start = new Date(); String out = ""; for(int a=0;a<1000000;a++) { out += "item" + a; } Date end = new Date(); MessageBar.error("str:" + (end.getTime() - start.getTime())); } // StringBuffer implementation private void sb() { Date start = new Date(); StringBuffer out = new StringBuffer(); for(int a=0;a<1000000;a++) { out.append("item" + a); } Date end = new Date(); MessageBar.error("sb:" + (end.getTime() - start.getTime())); } 

Results:

 str:1612 str:1788 str:1579 sb:1765 sb:1818 sb:1839 
+6
source share
2 answers

The next question is about stan229 and Bill Lizard's request.

It's really interesting how much the performance differs from browser to browser. For me, the question was which concatenation method to choose , and I got the answer I wanted. But here are more test results:

 chrome 10.0.648.204: str: 748 sb : 849 firefox 3.6.16: str: 1681 sb : 1861 ie 8: str: 2484 sb : 4094 opera 11.10 str: 802 sb : 792 

So, I got the answer: + operator gives better performance

My next question is what gives the best performance:

 int i=0; // this String result = String.valueOf(i); // or this String result = i + ""; 

will post this as soon as I do the test, or if you have an answer please write

+8
source

You can look at the gwt source code and see how StringBuffer / StringBuilder is emulated. GWT selects the best performance for string concatenation for browsers.

GWT 2.2.0 Source StringBuffer

A quick way to create strings using multiple add-ons. This is implemented using StringBufferImpl, which is selected with a delayed binding. Most of the methods will give the expected results ...

0
source

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


All Articles