I posted PowerShell, An Exercise in Species Barcoding and began coding up the metric for measuring the difference between two sequences of characters for the genomes. There are ~1250 sequences. Comparing each means 1,562,500 comparisons.
I tested empty loops with inner loops. Looping 500 times in each. The Inline C# version 1,500 times, doing three times as many loops as the the other and performing significantly faster.
It’s not unexpected that C# is fastest. It’s great that PowerShell provides the Add-Type cmdlet which can compile C# inline. Add-Type can do even more. You can compile VB.Net and F# too. Try help Add-Type –Examples at the command line and check out the possibilities.
ForEach Object
Function ForEachObject {
1..500 |
% {
1..500 |
% {
}
}
}
For Statement
Function ForStatement {
For($idx=0; $idx -lt 500; $idx++) {
For($idxX=0; $idxX -lt 500; $idxX++) {
}
}
}
ForEach Statement
Function ForEachStatement {
foreach($i in (1..500)) {
foreach($j in (1..500)) {
}
}
}
Inline C#
Function InLineCSharp {
Add-Type @"
public class Test {
public static void DoIt() {
for(int i=0; i < 1500; i++) {
for(int j=0; j < 1500; j++) {
}
}
}
}
"@
[Test]::Doit()
}





{ 3 comments… read them below or add one }
Actually I bet the for loop would be even worse if you made references to array indexes inside the loop. PowerShell has a lot of overhead for member access and it doesn’t have all the advantages of C#’s optimizer for doing things like caching the result of the Length property of an array.
Very interesting post! About 5 minutes of code updates and now I have a script running at 3/4 of the original processing time. I went from multiple ‘foreach objects’ using the pipeline to using foreach statements. I will keep this in mind when using PowerShell to parse large amounts of data.
Jason, another good reason not to use ForEach-Object naively in places where you could use loops is that you can’t use flow control statements like return/continue/etc the way you think they would behave. But watch using foreach in place of the pipeline if you’ve got a ton of data that you don’t want in memory all at once.