PowerShell Performance Tuning: A Helpful Script
This little gem may come in handy when you think you may need to do some performance tuning on your PowerShell script. I'll just let my brilliant script do all the talking:
--
function Help-PerformanceTuning
{
#this function helps me when I feel the urge to
#optimize for performance
$max = 4000
$regexObjects = @()
for($i=0; $i -lt ($max-1); $i++)
{
$regexObjects += [regex](Get-TwelveRandomCharacters)
}
echo "Congratulations! You've created $max Regex objects! That's totally, ridiculously wasteful! Now get back to work!"
}
--
It sure cures what's ailing me!
Astute readers may have noted that I call a "Get-TwelveRandomCharacters" function in there. Well, we should all be pleased to note that I further waste resources by instantiating twelve Random objects for each Regex object created! Let's do this:
--
function Get-TwelveRandomCharacters
{
$chars = @()
for ($i=0; $i -lt 12; $i++)
{
$r = new-object Random
$chars += [string][char][byte]($r.Next(1,26)+64)
}
[string]::Join("", $chars)
}
--
Awesome!