Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active June 23, 2023 15:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save santisq/0b00a5ad00e7614a04fc83465a6e4d9f to your computer and use it in GitHub Desktop.
stringbuilder vs addition assignment vs join op for strings
$tests = [ordered]@{
'addition assignment +=' = {
$string = ''
for ($i = 0; $i -lt $args[0]; $i++) {
$string += 'A'
}
}
'join op' = {
$string = -join @(
for ($i = 0; $i -lt $args[0]; $i++) {
'A'
}
)
$string
}
'stringbuilder' = {
$sb = [System.Text.StringBuilder]::new()
for ($i = 0; $i -lt $args[0]; $i++) {
$sb = $sb.Append('A')
}
$sb.ToString()
}
}
1..10 | ForEach-Object {
$arg = $_ * 10kb
foreach ($test in $tests.GetEnumerator()) {
[pscustomobject]@{
Iterations = $arg
Test = $test.Key
Time = (Measure-Command { & $test.Value $arg }).TotalMilliseconds
}
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
} | Format-Table -AutoSize

.NET - PowerShell 7.4.0-preview.3

Iterations Test                      Time
---------- ----                      ----
     10240 addition assignment +=   78.09
     10240 join op                  24.42
     10240 stringbuilder            16.55
     20480 addition assignment +=  204.77
     20480 join op                  29.72
     20480 stringbuilder            13.91
     30720 addition assignment +=  455.86
     30720 join op                  46.78
     30720 stringbuilder            18.59
     40960 addition assignment +=  802.48
     40960 join op                  60.78
     40960 stringbuilder            20.91
     51200 addition assignment += 1566.67
     51200 join op                  78.73
     51200 stringbuilder            32.50
     61440 addition assignment += 1840.41
     61440 join op                  97.32
     61440 stringbuilder            33.14
     71680 addition assignment += 2722.61
     71680 join op                 109.78
     71680 stringbuilder            43.32
     81920 addition assignment += 3500.62
     81920 join op                 185.21
     81920 stringbuilder            53.56
     92160 addition assignment += 4579.31
     92160 join op                 173.89
     92160 stringbuilder            52.84
    102400 addition assignment += 5301.13
    102400 join op                 187.67
    102400 stringbuilder            59.09

.NET Framework - Windows PowerShell 5.1

Iterations Test                         Time
---------- ----                         ----
     10240 addition assignment +=   138.1825
     10240 join op                    58.793
     10240 stringbuilder             54.5137
     20480 addition assignment +=   146.6317
     20480 join op                    56.023
     20480 stringbuilder              6.7565
     30720 addition assignment +=   302.6283
     30720 join op                   86.9992
     30720 stringbuilder              9.3392
     40960 addition assignment +=   527.1881
     40960 join op                  116.7398
     40960 stringbuilder             10.7381
     51200 addition assignment +=  1773.8314
     51200 join op                  158.9269
     51200 stringbuilder             16.2377
     61440 addition assignment +=  3421.4698
     61440 join op                  179.7173
     61440 stringbuilder              20.603
     71680 addition assignment +=  5453.7792
     71680 join op                    211.19
     71680 stringbuilder             21.7919
     81920 addition assignment +=  7794.6372
     81920 join op                  259.4236
     81920 stringbuilder             22.8904
     92160 addition assignment += 10294.9423
     92160 join op                  295.1333
     92160 stringbuilder             29.7306
    102400 addition assignment += 14991.0868
    102400 join op                   361.895
    102400 stringbuilder             34.6465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment