Created
December 21, 2018 09:16
-
-
Save nohwnd/22787d817cea929410fe5077a36f4ba2 to your computer and use it in GitHub Desktop.
Deffered evaluation for logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Logging verbose info to screen is useful for debugging | |
# but the info is hidden 99% of the time. Problem is that | |
# evaluating a string to get the verbose message will run | |
# even if we never actually print it, adding overhead to | |
# every run, not just runs that are verbose. | |
# to avoid this passing a script block that evaluates to the | |
# message string conditionally seems to work well | |
# writes a string to screen | |
function v ([String]$m) { | |
if ($writeToScreen) { | |
Write-Host $m -NoNewline | |
} | |
} | |
# adds optional delay that simulates | |
# some computation of info for the verbose message | |
function DoWork () { | |
if ($delay -eq 0) { | |
} | |
elseif ($delay -eq 1) { | |
$p = Get-Process -Id $PID | |
} | |
else { | |
$i = Get-Process | Where { $_.Name -eq 'Idle' } | |
} | |
} | |
# evaluates string from scriptblock and then | |
# writes is to screen | |
function v2 ([ScriptBlock]$m) { | |
if ($writeToScreen) { | |
Write-Host (&$m) -NoNewline | |
} | |
} | |
# does nothing but a function call | |
function v3 { } | |
# runs each function 1000 times | |
# first with output to screen | |
# (there most overhead is coming from | |
# Write-Host but the performance of functions | |
# that actually write to screen seem comparable | |
# with scriptblock being slightly slower | |
# in the second run nothing is written to the screen | |
# and there the scriptblock wins, by some 10ms | |
# it also adds a progresively long delay that shows how | |
# the overhead changes when some real work is done to produce | |
# the verbose message | |
# normal example from running this code: | |
# write to screen: True, (delay 0): 1105.4102, 1239.677, 74.5671 | |
# write to screen: False, (delay 0): 143.242, 89.707, 64.8482 | |
# write to screen: True, (delay 1): 4093.0652, 4179.2973, 68.2799 | |
# write to screen: False, (delay 1): 2480.0881, 86.4388, 65.0745 | |
# write to screen: True, (delay 2): 11269.3723, 12252.0789, 66.3764 | |
# write to screen: False, (delay 2): 9578.4522, 86.4895, 70.4621 | |
# extreme example of a case where I output cimclass to the console | |
# write to screen: True, (delay 0): 1085.6265, 1154.473, 80.5295 | |
# write to screen: False, (delay 0): 146.2093, 90.4551, 64.5146 | |
# write to screen: True, (delay 1): 4192.0924, 4076.2387, 71.131 | |
# write to screen: False, (delay 1): 2416.7699, 89.4839, 65.3627 | |
# write to screen: True, (delay 2): 54331.3003, 53858.9505, 64.5398 | |
# write to screen: False, (delay 2): 50871.7438, 90.6663, 66.7613 | |
$name = "Jakub" | |
$r = 0,1,2 | foreach { | |
$delay = $_ | |
$true, $false | foreach { | |
$writeToScreen = $_ | |
$t1 = (Measure-Command { | |
1..1000 | % { v "Hello,$(doWork) $name"} | |
}).TotalMilliseconds | |
$t2 = (Measure-Command { | |
1..1000 | % { v2 { "Hello,$(doWork) $name" } } | |
}).TotalMilliseconds | |
$t3 = (Measure-Command { | |
1..1000 | % { v3 } | |
}).TotalMilliseconds | |
"write to screen: $writeToScreen, (delay $delay): $t1, $t2, $t3" | |
} | |
} | |
"`n" | |
$r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment