Last active
August 29, 2015 14:21
-
-
Save ChaseFlorell/041a39eb1338ed88335a to your computer and use it in GitHub Desktop.
Powershell inner collection iteration performance benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$complexObject = @{ | |
test = "test" | |
guid = [guid]::NewGuid() | |
int = [int]::MaxValue | |
str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." | |
arr = @(1..100) | |
} | |
$complexCollection = @() | |
$sw = [System.Diagnostics.Stopwatch]::StartNew() | |
Write-Host 'add the complex object to the complex array 10000 times.' | |
(1..10000) | % { $complexCollection += $complexObject } | |
Write-Host "adding the object 10000 times took $($sw.Elapsed)." | |
$sw.Restart() | |
$complexCollection | %{ $_.arr | % {$x = $_} }; $x = $null | |
$percentLoop = $($sw.Elapsed) | |
$sw.Restart() | |
$complexCollection | %{ | |
foreach($var in $_.arr){ | |
$x = $var | |
} | |
}; $x = $null | |
$foreachLoop = $($sw.Elapsed) | |
$sw.Restart() | |
foreach($outer in $complexCollection){ | |
foreach($var in $outer.arr){ | |
$x = $var | |
} | |
} | |
$fullForEach = $($sw.Elapsed) | |
$sw.Stop() | |
@{'ForEach' = $foreachLoop; 'percent (%)' = $percentLoop; 'full ForEach' = $fullForEach} | ft -AutoSize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add the complex object to the complex array 10000 times. | |
adding the object 10000 times took 00:00:03.5356614. | |
Name Value | |
---- ----- | |
full ForEach 00:00:01.1445330 | |
ForEach 00:00:01.5091856 | |
percent (%) 00:00:20.0787455 | |
A full foreach loop is definitively faster. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment