Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Created November 30, 2014 01:48
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 Jaykul/1877b4417a206b74c60c to your computer and use it in GitHub Desktop.
Save Jaykul/1877b4417a206b74c60c to your computer and use it in GitHub Desktop.
The cost of Write-Debug when you're not debugging
function Test-DebugPreference {
[CmdletBinding()]
param($count = 1e5)
$Counter = 0
for($i = 0; $i -lt $count; $i++) {
if($DebugPreference -gt "SilentlyContinue") {
$Counter += 1
Write-Debug "The Counter is $Counter"
}
}
}
function Test-WriteDebug {
[CmdletBinding()]
param($count = 1e5)
$Counter = 0
for($i = 0; $i -lt $count; $i++) {
Write-Debug "The Counter is $(($Counter += 1))"
}
}
$DebugPreference = "SilentlyContinue"
Measure-Command { Test-DebugPreference } # ~ 441 ms on my hardware
Measure-Command { Test-WriteDebug } # ~ 5995 ms on my hardware
$DebugPreference = "Continue"
Measure-Command { Test-DebugPreference } # ~47906 ms on my hardware
Measure-Command { Test-WriteDebug } # ~46122 ms on my hardware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment