Reputation:
If i run the following:
Measure-Command -Expression {gci -Path C:\ -Recurse -ea SilentlyContinue | where Extension -eq ".txt"}
Measure-Command -Expression {gci -Path C:\ -Filter *.txt -Recurse -ea SilentlyContinue}
The second expression is always faster that the first one, im guessing its because it doesnt have to use the pipeline.
I thought maybe in the Pipeline method PowerShell recursed my drive and passed a collection of objects to the where clause, that would have to iterate through the items again, but i ruled that out, because if you run the first expression you can see it return output as it is recursing. So why is the Pipeline method slower?
Upvotes: 2
Views: 626
Reputation: 16792
Shay's answer is totally correct. I wanted to touch on your secondary question a bit, though. Here's what's happening under the hood in the pipeline:
gci -Path C:\ -Recurse -ea SilentlyContinue | where Extension -eq ".txt"
gci
will start searching for files and directories at or under c:\
, any extension. As it finds each one, that one result is passed on to Where-Object
, which will discard it if the extension is not .txt
. If the extension is .txt
, that object is passed on in the pipeline, and out to the console (or to a variable, or whatever). Then gci
will continue its search, when it finds the next file, it will pass it on, etc. So although it might take a couple minutes to search the entire c:\
drive, you get partial results streamed back to you almost immediately, since the pipeline operates one object at a time.
What is not happening is that gci
does the full disk search all at once, then hands the complete results set to Where-Object
only when it's complete.
Upvotes: 0
Reputation: 126712
Using Where-Object
is always slower than using the built in parameters of the left hand side command. You first bring ALL objects to your shell and only then starts filtering them (client side filtering).
With regard to the -Filter parameter, it works faster because it performs on the provider level (server side filtering), objects are checked once accessed and you get back only the ones that match your criteria.
Upvotes: 5