16 years ago |
16 years ago |
If you try 1000 iterations, StringBuilder will be clearly on top, and if you try 100,000 iterations, there will be no contest.
Also, the methodology in this test is completely broken. I wouldn't be surprised if the compiler partially or completely optimizes out the loops using immutable strings, since you don't use the result anywhere. To design a correct test for a micro-optimization like this you need to check the generated code in each instance (at a minimum.)
EDIT: I just noticed the strbldr.ToString() as well, which completely defeats the purpose even further. Please do more work before jumping to conclusions and publishing them to an audience. How did this make it to the front page?
Running it several times showed that the 20 cycle loop was fairly close, where the 20,000 loop was not even near close.
0.0000089 vs. 0.0000092
and 0.9846729 vs. 0.0006852
Edit: I also changed them both to add a fixed string, since I don't have enough memory for a 4 * 2^20,000 length string.