Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Created March 29, 2020 12:10
Show Gist options
  • Save nohwnd/625e53dcf4e9ffad6bd5cee185f61e3a to your computer and use it in GitHub Desktop.
Save nohwnd/625e53dcf4e9ffad6bd5cee185f61e3a to your computer and use it in GitHub Desktop.
Cross function boundary perf
# Quickly measuring if it is better to call function in foreach loop or gerate the array and pass it in the function once
# and then iterate over the array inside of the function
"`n"
# cross function boundary 10,000 times
$script:b = 0
function a ($a) { $script:b += $a }
$sw = [Diagnostics.StopWatch]::StartNew()
foreach ($_ in 1..10000) {
a 1
}
"10k function:`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
# cross function boundary once with array of 10,000 items
$script:b = 0
function a ($a) {
foreach ($i in $a) {
$script:b += $i
}
}
$sw = [Diagnostics.StopWatch]::StartNew()
$a = foreach ($_ in 1..10000) {
1
}
a $a
"1 function:`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
# cross cmdlet boundary 10,000 times
$script:b = 0
function a {
[CmdletBinding()]
param ($a)
$script:b += $a
}
$sw = [Diagnostics.StopWatch]::StartNew()
foreach ($_ in 1..10000) {
a 1
}
"10k cmdlet:`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
# cross cmdlet boundary once with array of 10,000 items
$script:b = 0
function a {
[CmdletBinding()]
param ($a)
foreach ($i in $a) {
$script:b += $i
}
}
$sw = [Diagnostics.StopWatch]::StartNew()
$a = foreach ($_ in 1..10000) {
1
}
a $a
"1 cmdlet:`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
# cross sb boundary 10,000 times
$script:b = 0
$sb = { param ($a) $script:b += $a }
$sw = [Diagnostics.StopWatch]::StartNew()
foreach ($_ in 1..10000) {
& $sb 1
}
"10k sb:`t`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
# cross sb boundary once with array of 10,000 items
$script:b = 0
$sb = {
param($a)
foreach ($i in $a) {
$script:b += $i
}
}
$sw = [Diagnostics.StopWatch]::StartNew()
$a = foreach ($_ in 1..10000) {
1
}
& $sb $a
"1 sb:`t`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
# cross sb boundary with hashtable lookup 10k times
$script:b = 0
function a {
param($a)
foreach ($i in $a) {
$script:b += $i
}
}
$hash = Get-Command -CommandType function | Select -First 400 | foreach { $hash = @{}} { $hash.($_.Name) = $_} { $hash }
$sw = [Diagnostics.StopWatch]::StartNew()
$a = foreach ($_ in 1..10000) {
& $hash.a 1
}
"10k hash:`t$script:b iter, $($sw.ElapsedMilliseconds) ms"
10k function: 10000 iter, 312 ms
1 function: 10000 iter, 8 ms
10k cmdlet: 10000 iter, 321 ms
1 cmdlet: 10000 iter, 10 ms
10k sb: 10000 iter, 200 ms
1 sb: 10000 iter, 13 ms
10k hash: 10000 iter, 213 ms
PS C:\Projects\pester>
10k function: 10000 iter, 277 ms
1 function: 10000 iter, 9 ms
10k cmdlet: 10000 iter, 318 ms
1 cmdlet: 10000 iter, 8 ms
10k sb: 10000 iter, 198 ms
1 sb: 10000 iter, 12 ms
10k hash: 10000 iter, 219 ms
PS C:\Projects\pester>
10k function: 10000 iter, 313 ms
1 function: 10000 iter, 9 ms
10k cmdlet: 10000 iter, 336 ms
1 cmdlet: 10000 iter, 9 ms
10k sb: 10000 iter, 230 ms
1 sb: 10000 iter, 12 ms
10k hash: 10000 iter, 213 ms
PS C:\Projects\pester>
10k function: 10000 iter, 311 ms
1 function: 10000 iter, 8 ms
10k cmdlet: 10000 iter, 344 ms
1 cmdlet: 10000 iter, 9 ms
10k sb: 10000 iter, 221 ms
1 sb: 10000 iter, 10 ms
10k hash: 10000 iter, 217 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment