Wednesday, 16 May 2012

Performance of string manipulation - part two

In part one I looked at the different ways of storing a string during string manipulation, and found that using a variable was by far quicker than using a field.  In this next part, I'm going to look at the different ways of building up the string.  I can think of only two different ways...

  • Concatenation (using $concat)
  • Indirection (eg. "%%string1%%string2%%%")

So I put these to the test with the following four strings...

"01234567890 "
"abcdefghijklmnopqrstuvwxyz "
"01234567890 "

...holding the value in a local variable and iterating 20,000,000 times...

  • Concatenation = 01:03.08, 01:03.71, 01:03.t64 (just over 1 minute)
  • Indirection = 01:02.01, 01:01.74, 01:01.98 (just over 1 minute)

The difference is almost negligible, as I had to go to so many iterations before it could be detected.  Indirection does pip concatenation to the post though, by a whisker.

I then decided to try building up a larger string, by starting with the first string and adding the other three, then taking that string and adding the other three again, and so on.  This time I also used a local variable but iterating only 20,000 times...

  • Concatenation = 00:56.41, 00:56.02, 00:56.37 (just under 1 minute)
  • Indirection = 00:55.61, 00:56.96, 00:56.92 (just under 1 minute)

Again the difference is pretty negligible, proving that in this case, size really doesn't matter.

Summary: Using concatenation or indirection doesn't really make a difference.

No comments:

Post a Comment