Skip to content

Instantly share code, notes, and snippets.

@smaglio81
Last active August 8, 2020 22:38
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 smaglio81/6cba4a3fe837bb3c3328b60afb61f8e4 to your computer and use it in GitHub Desktop.
Save smaglio81/6cba4a3fe837bb3c3328b60afb61f8e4 to your computer and use it in GitHub Desktop.
function RangeOperatorInFunc($Value) {
switch($Value) {
{0..24 -contains $_} { $a = 0 }
{25..49 -contains $_} { $a = 1 }
{50..74 -contains $_} { $a = 2 }
{75..99 -contains $_} { $a = 3 }
}
}
$gr1 = 0..24;
$gr2 = 24..49;
$gr3 = 50..74;
$gr4 = 75..99;
function CachedValuesNotInScope($Value) {
switch($Value) {
{$gr1 -contains $_} { $a = 0 }
{$gr2 -contains $_} { $a = 1 }
{$gr3 -contains $_} { $a = 2 }
{$gr4 -contains $_} { $a = 3 }
}
}
$global:hr1 = 0..24;
$global:hr2 = 24..49;
$global:hr3 = 50..74;
$global:hr4 = 75..99;
function CachedValuesWithScopedPrefixed($Value) {
switch($Value) {
{$global:hr1 -contains $_} { $a = 0 }
{$global:hr2 -contains $_} { $a = 1 }
{$global:hr3 -contains $_} { $a = 2 }
{$global:hr4 -contains $_} { $a = 3 }
}
}
Write-Host "All Times in Ticks (from Measure-Command)`r`n"
$r = New-Object System.Random
#$r.next(100)
function BaselineFunctionWithoutSwitch($Value) {
$a = 0
}
$baseFuncTicks = @()
for($i = 0; $i -lt 10000; $i++) {
$k = $r.Next(100)
$m = Measure-Command { BaselineFunctionWithoutSwitch($k) }
$baseFuncTicks += @($m)
}
$baseAvgTicks = ($baseFuncTicks.Ticks | Measure-Object -Average).Average
"Calling a Function Overhead = $baseAvgTicks`r`n"
$timeComparisons = @()
function New-TC($Description, $Measure, $BaseAvgTicks) {
$ticks = ($Measure.Ticks | Measure-Object -Average).Average
$switchTicks = $ticks - $BaseAvgTicks
return [PSCustomObject] @{
Description = $Description
'Switch Avg Execution Time' = $switchTicks
'Total Avg Execution Time' = $ticks
}
}
$ftimes = @()
for($i = 0; $i -lt 10000; $i++) {
$k = $r.Next(100)
$m = Measure-Command { RangeOperatorInFunc($k) }
$ftimes += @($m)
}
$timeComparisons += @(New-TC -Description "Range Operator in Func" -Measure $ftimes -BaseAvgTicks $baseAvgTicks)
$gtimes = @()
for($i = 0; $i -lt 10000; $i++) {
$k = $r.Next(100)
$m = Measure-Command { CachedValuesNotInScope($k) }
$gtimes += @($m)
}
$timeComparisons += @(New-TC -Description "Cached Values not in Func Scope" -Measure $gtimes -BaseAvgTicks $baseAvgTicks)
$htimes = @()
for($i = 0; $i -lt 10000; $i++) {
$k = $r.Next(100)
$m = Measure-Command { CachedValuesWithScopedPrefixed($k) }
$htimes += @($m)
}
$timeComparisons += @(New-TC -Description "Explicitly Scoped Cached Values" -Measure $htimes -BaseAvgTicks $baseAvgTicks)
$itimes = @()
for($i = 0; $i -lt 10000; $i++) {
$k = $r.Next(100)
$m = Measure-Command {
switch($Value) {
{0..24 -contains $_} { $a = 0 }
{25..49 -contains $_} { $a = 1 }
{50..74 -contains $_} { $a = 2 }
{75..99 -contains $_} { $a = 3 }
}
}
$itimes += @($m)
}
$timeComparisons += @(New-TC -Description "Range Operator with No Func" -Measure $times -BaseAvgTicks 0)
$timeComparisons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment