Repository navigation
Description
Write-Host has a 150ms delay (~50ms on Linux) when called from a profile, but it doesn't if called directly. I thought maybe this would be due to CPU startup time but it's not, it's consistent every time, rarely wavers.
This is important because custom prompts and whatnot use write-host, and so a delay for no reason makes powershell feel more "sluggish" on startup.
Steps to reproduce
Save the following to ~\desktop\cpscript.ps1. Run ~\desktop\cpscript.ps1 from commandline. Normal expected behavior.
Add ~\desktop\cpscript.ps1 to profile.ps1. Start powershell normally. Second write-host takes +150ms to run, the remainder run normally.
$cpnum = 0
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
"Checkpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
"Checkpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
"Checkpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
"Checkpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
"Checkpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
write-host "WHCheckpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
write-host "WHCheckpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
write-host "WHCheckpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
write-host "WHCheckpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"
write-host "WHCheckpoint $($cpnum++;$cpnum): $($stopwatch.elapsed.milliseconds)ms"Doesn't have to be write host, can also be write-output, write-warning, etc. and it has the same effect.
Expected behavior
What happens when you run ~\desktop\CPScript.ps1 in a normal session
Checkpoint 1: 0ms
Checkpoint 2: 0ms
Checkpoint 3: 1ms
Checkpoint 4: 1ms
Checkpoint 5: 1ms
WHCheckpoint 6: 2ms
WHCheckpoint 7: 3ms
WHCheckpoint 8: 3ms
WHCheckpoint 9: 4ms
WHCheckpoint 10: 5ms
Actual behavior
What happens when you run it called from the profile
Checkpoint 1: 5ms
Checkpoint 2: 10ms
Checkpoint 3: 10ms
Checkpoint 4: 11ms
Checkpoint 5: 11ms
WHCheckpoint 6: 11ms
WHCheckpoint 7: 178ms
WHCheckpoint 8: 179ms
WHCheckpoint 9: 179ms
WHCheckpoint 10: 180ms
Linux
Checkpoint 1: 5ms
Checkpoint 2: 12ms
Checkpoint 3: 12ms
Checkpoint 4: 12ms
Checkpoint 5: 12ms
WHCheckpoint 6: 12ms
WHCheckpoint 7: 75ms
WHCheckpoint 8: 75ms
WHCheckpoint 9: 76ms
WHCheckpoint 10: 76ms
Same behavior less pronounced.
Appears to happen on PS5.1 as well
Checkpoint 1: 4ms
Checkpoint 2: 8ms
Checkpoint 3: 8ms
Checkpoint 4: 9ms
Checkpoint 5: 9ms
WHCheckpoint 6: 10ms
WHCheckpoint 7: 180ms
WHCheckpoint 8: 181ms
WHCheckpoint 9: 186ms
WHCheckpoint 10: 187ms
Note that the first write-host works just fine, so whatever is introducting the delay is code after write-host writes to console.
Environment data
Name Value
---- -----
PSVersion 6.1.0-preview.4
PSEdition Core
GitCommitId 6.1.0-preview.4
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Happens in PS5.1 as well.
Activity
lzybkr commentedon Aug 21, 2018
I have some ideas on where that delay is coming from, but I'll take this opportunity to share some tools to help analyze the performance, see this PR.
The regions of interest defined in that PR should make it relatively simple to understand where that delay is coming from.
Note that these events are available in PS5.1 as well - I added this instrumentation to help make major improvements to the startup scenario - PS5.1 is much faster in your scenario than PS5.0.
JustinGrote commentedon Aug 22, 2018
@izybkr Interesting PR, thanks I'll see if I can use that to dial it in a bit.
I didn't try a PS5.0 so I can't comment on how "fast" it is relative to the other tests, I only did PS Core and PS 5.1
JustinGrote commentedon Aug 2, 2019
Jason Shirk (@lzybkr) Just a bump that this is still an issue, tested all the way up to Windows Insider and latest pwsh preview. Workaround is to not use write-host at all in your prompt (which is where the slowness was most visible) but to use ANSI escape sequences only, which is more viable now with Windows 1809 than it was in the past.
I have observed that it does not occur when running -version 2 parameter, I currently can't verify 3/4/5 to see if there's a point it was introduced, but it may be due to the new Write-Information and Write-Host layer that was added in 5
JustinGrote commentedon Nov 16, 2023
I'm going to close as this doesn't seem to reproduce in newer windows/PS7+