Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created November 10, 2019 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/0b0edd461f6acb07f09c317e53645342 to your computer and use it in GitHub Desktop.
Save IISResetMe/0b0edd461f6acb07f09c317e53645342 to your computer and use it in GitHub Desktop.
Add-Type @'
public struct StringWrapper
{
string s;
public StringWrapper(string s){ this.s = s; }
public void Add(string suffix)
{
s += suffix;
}
public void Concat(string suffix)
{
s = string.Concat(s, suffix);
}
public override string ToString()
{
return s;
}
}
'@
# use a stopwatch to measure performance
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# create a string wrapper
$text = [StringWrapper]::new("")
# appending a string often
# using foreach and avoiding the pipeline
foreach($_ in (1..100000))
{
# replace operator += with Add():
$text.Add("working on $_`r`n")
}
# check results:
$report = 'composed string of length {0} in {1:n2} seconds'
$report -f $text.Length, $stopwatch.Elapsed.TotalSeconds
# use a stopwatch to measure performance
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# create a string wrapper
$text = [StringWrapper]::new("")
# appending a string often
# using foreach and avoiding the pipeline
foreach($_ in (1..100000))
{
# replace operator += with Add():
$text.Concat("working on $_`r`n")
}
# check results:
$report = 'composed string of length {0} in {1:n2} seconds'
$report -f $text.Length, $stopwatch.Elapsed.TotalSeconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment