Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

What's the most efficient string concatenation method?

$
0
0

I guess this really isn't a question, but the result of a simple test. I wanted to see if there were significant differences between the various methods of string concatenation. It turns out there are.

Here's the test script. Each of the methods produces a string of 120,000 characters by concatenating two strings of six characters to an existing string in a loop executed 10,000 times. Each method is run 20 times.

$outerlooptimes = 20
$innerlooptimes = 10000
$l1 = @()
$l2 = @()
$l3 = @()
$l4 = @()
$l5 = @()
1..$outerlooptimes | foreach {
    $finalstring = ""
    $x = Measure-Command {
        1..$innerlooptimes | Foreach {
            $finalstring += 'abcdef' + 'hijklm'
        }

    }
    $l1 += $x.TotalMilliseconds

    $finalstring = ""
    $x = Measure-Command {
        1..$innerlooptimes | Foreach {
            $finalstring += [String]::Join("",'abcdef','hijklm')
        }

    }
    $l2 += $x.TotalMilliseconds

    $finalstring = ""
    $x = Measure-Command {
        1..$innerlooptimes | Foreach {
            $finalstring += [System.String]::Concat('abcdef','hijklm')
        }
    }
    $l3 += $x.TotalMilliseconds

    $finalstring = ""
    $x = Measure-Command {
        1..$innerlooptimes | Foreach {
            $finalstring += '{0}{1}' -f 'abcdef','hijklm'
        }
    }
    $l4 += $x.TotalMilliseconds
    $finalstring = ""
    $sb = New-Object System.Text.StringBuilder("")
    $x = Measure-Command {
        1..$innerlooptimes | foreach {
            [void]$sb.Append('abcdef').Append('hijklm')
        }
    }
    $l5 += $x.TotalMilliseconds

}

$l1 | measure-object  -average |Select @{n="Method";e={"+ operator"}}, Average
$l2 | Measure-Object  -average |Select @{n="Method";e={"Join"}}, Average
$l3 | Measure-Object  -average |Select @{n="Method";e={"[String]::Concat"}},Average
$l4 | Measure-Object  -average |Select @{n="Method";e={"-Format"}},Average
$l5 | Measure-Object  -average |Select @{n="Method";e={"StringBuilder"}},Average

These are the results, taken from running the script five separate times.

Method              Average
------              -------+ operator       504.850215
Join             519.020905
[String]::Concat  543.36916
-Format           521.34203
StringBuilder      65.90402


Method              Average
------              -------
+ operator        523.90415
Join             559.787155
[String]::Concat 521.444215
-Format           548.81088
StringBuilder     59.749685

Method              Average
------              -------
+ operator        510.22507
Join               546.3793
[String]::Concat 555.216135
-Format          547.597615
StringBuilder      69.66882

Method              Average
------              -------
+ operator        510.22507
Join               546.3793
[String]::Concat 555.216135
-Format          547.597615
StringBuilder      69.66882

Method              Average
------              -------
+ operator        536.09595
Join              530.90099
[String]::Concat 558.391205
-Format          532.526615
StringBuilder      59.97045

I'd conclude that if you have long-running loops that [String]::StringBuilder is why way to go. In "normal" scripts that don't do a lot of string concatenation I don't think there's going to be any significant differences in running times no matter which of the methods you pick.


--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>