PowerShell S2E65 (For/Foreach/Foreach-object/.foreach)

Описание к видео PowerShell S2E65 (For/Foreach/Foreach-object/.foreach)

In this video I want to create awareness on the differences between the foreaches in powershell.
Make sure you understand the differences well.

*powershell
*learn powershell
*windows
*windows powershell

Code :

$MaxCount = 500000
$MyArrayList = [System.Collections.ArrayList]@()

#Suboptimal (less memory intensive)
Measure-Command {(0..$MaxCount).foreach({$MyArrayList.Add($_)})} | select TotalSeconds
$MyArrayList.Clear()

Measure-Command {(0..$MaxCount) | ForEach-Object {$MyArrayList.Add($_)}} | select TotalSeconds
$MyArrayList.Clear()

#OK (best if you dont have much memory)
Measure-Command {for ($i = 0; $i -le $MaxCount; $i++){$MyArrayList.Add($i) }}
$MyArrayList.Clear()

#optimal! If you have memory available, always go for this one.
Measure-Command {foreach($i in 0..$MaxCount){ $MyArrayList.Add($i)}} | select TotalSeconds

Комментарии

Информация по комментариям в разработке