Got confused about Strings

By patrickwellink
May 26, 2005

I read somewhere on this site the do’s and dont’s about strings…Because I use the + a lot I did run some tests….If you have a construct like this :

Eventlogentry = New Eventlogentry(“There was a failure executing receive location “ +  config.locationname + “. Please review the configuration“)

It doesn’t matter  what you use cause it is only a single instance. You will see only a difference between string.format, string.concat and + if you do a zillion operations.

But remember this is only true if you only instantiate the resultstring. If you are adding to a string within a loop, use the stringbuilder.

Here are some timings :

Concat took        :660.9504
++++++ took        :650.936
Stringbuilder took :1432.0592
String format took :1071.5408

The size of the strings could have imact.  I did one million iterations and the difference between the stuff is minimal. So frankly if you use it only to construct a string to display somewhere… don’t bother use whatevber you like best.

If you”re in a loop use the stringbuilder !

The code I used to produce these timings is in a reply on this post

Comments: 1

  1. ernst says:

    Sorry, maar dit is wat we noemen: "een open deur intrappen" 😉

Comments are closed.

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta