Introduction:
Efficiency is key when working with PowerShell, and maximizing performance is a common goal for script developers. In this post, we'll explore various techniques for retrieving the maximum number from arrays of different sizes, comparing well-known approaches like Measure-Object, Sort-Object and a more mathematical method.
Methods:
-
Measure-Object:
To retrieve the maximum number using Measure-Object, the following command is used:
(@(1,3,55,69,13,37)| Measure-Object -Maximum).Maximum
-
Sort-Object:
Another approach involves sorting the array and extracting the highest number from the last index:
(@(1,3,55,69,13,37)| Sort-Object)[-1]
-
Mathematical Approach:
Utilizing the [System.Math] class and its Max method requires a loop through the array:
$Numbers = (@(1,3,55,69,13,37)
$maxValue = $Numbers[0] #we need to start with an number
foreach($num in $Numbers){
$maxValue = [System.Math]::Max($Num,$maxValue)
}
Performance Testing:
For a comprehensive performance comparison, three arrays of different sizes were created:
$NumbersLarge = get-random -min 0 -max 1gb -count 20mb
$NumbersMedium = get-random -min 0 -max 1gb -count 1mb
$NumbersSmall = get-random -min 0 -max 1gb -count 100kb
Results:
Small List:
Technique Time RelativeSpeed Throughput
--------- ---- ------------- ----------
Math 00:00:00.144915 1x 34.5/s
Measure 00:00:00.602446 4.16x 8.3/s
Sort 00:00:02.053357 14.17x 2.44/s
Medium List:
Technique Time RelativeSpeed Throughput
--------- ---- ------------- ----------
Math 00:00:01.377477 1x 3.63/s
Measure 00:00:11.163644 8.1x 0.45/s
Sort 00:00:31.034414 22.53x 0.16/s
Large List:
Technique Time RelativeSpeed Throughput
--------- ---- ------------- ----------
Math 00:00:26.392599 1x 0.19/s
Measure 00:01:57.695910 4.46x 0.04/s
Sort 00:18:38.731559 42.39x 0/s
Conclusion:
The [System.Math]::Max() method consistently outperforms Measure-Object and Sort-Object, particularly for larger datasets. Although its implementation may be less intuitive, encapsulating it in a function enhances usability.
Function Implementation:
function Get-MaxNumber {
param (
[int[]]Numbers
)
$MaxValue = $Numbers[0]
foreach($num in $Numbers){
$MaxValue = [System.Math]::Max($MaxValue,$num)
}
return $MaxValue
}
In scenarios where milliseconds matter and the primary goal is to retrieve the maximum number, the targeted solution using [System.Math]::Max() proves to be a powerful choice.
If you have any thoughts or feedback on this topic, feel free to share them with me on Twitter at Christian Ritter.
Best regards, Christian