samedi 25 août 2018

Powershell executionspeed Improvment

i`ve played around with some sorting algorithms written in powershell last night. Beforehand i hade to generate a Array of random ints so i have something i can sort. However, the time to generate the Array took me too long and so i improved my code to run as fast as possible. My final code looks like this:

$max = 1000000
$RndNumberArr = New-Object System.Collections.ArrayList
$ran = New-Object Random
for ($i = 0; $i -lt $max; $i++)
{ 
    [void]$RndNumberArr.Add($ran.Next(0,$max))    
}

I`ve noticed that using an ArrayList, is faster then using an Array with fixed Size (which dosent make much sense to me, is there something faster?) Also, using the .NET method random.Next(), instead of the CMDLet Get-Random improves the speed a good amount aswell. Are there any downsides of using the .NET methods? ALso i think that, using

[void]$RndNumberArr.Add($ran.Next(0,$max)) 

to prevent the Array.Add output ist slightly faster then using

$RndNumberArr.Add($ran.Next(0,$max)) > $null

am i right? The CMDLet is with a clear timedifference the slowest.

$RndNumberArr.Add($ran.Next(0,$max)) | Out-Null

Does my Computerhardware (CPU especially) effect the powershell execution speed or is the speed capped at some point? If so, is there a way to enhance the assigned system ressources for a speed improvment? Thanks a lot.




Aucun commentaire:

Enregistrer un commentaire