top | item 932094

(no title)

unknown | 16 years ago

discuss

order

mquander|16 years ago

This test is really bad. 3 iterations? 20 iterations? Common sense says that the performance difference in these cases will be immaterial anyway barring some bizarre scenario, so you should write whatever is clearer.

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?

lanstein|16 years ago

other than that, this is an excellent, well thought-out study. ;)

joe_adk|16 years ago

Just for fun I removed the extra concat on the stringbuilder and upped the cycles to 20 and 20,000.

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.