Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.