Sunday, May 7, 2017

Get-ChildItem can be far slower than classic dir

A Super User member noticed that PowerShell's directory listing command Get-ChildItem is dramatically slower than a simple dir in the normal command prompt. He tried launching the command prompt from PowerShell to do the listing (cmd /c dir) and found no noticeable difference in speed.

The explanation is that starting processes is an expensive operation. For short listings, the process launch overwhelms the cost of doing the PowerShell listing. When I tried it with long listings, like in System32, the command processor beats PowerShell hugely: by an order of magnitude, at least on my machine.

I suspect the PowerShell slowness comes from managed overhead: the creation of .NET FileInfo objects and the filling of all their properties. Then to be displayed in the console (assuming the user is just browsing around), those objects have to be formatted, while the dir output is already a string.

No comments:

Post a Comment