My recent post on spheres got me wondering about the time for cube calculation.
Defining a variable
PS> $r = 2.37
I can calculate the cube by multiplying $r by itself 3 times or by using the Power function on the Math class.
Starting with the simple calculation
PS> Measure-Command -Expression {$r * $r * $r}
PowerShell v7 RC2 took 18 milliseconds; PowerShell v6.2.4 took 9 milliseconds and Windows PowerShell 8 milliseconds
Using the math function
PS> Measure-Command -Expression {[math]::Pow($r, 3)}
PowerShell v7 RC 2 took 24 milliseconds; PowerShell v6.2.4 took 18 milliseconds and Windows PowerShell took 14 milliseconds.
PowerShell v7 took longer because its a release candidate rather than release code so has extra stuff to do – as technical as I want to get on that one.
PowerShell v6.2.4 and Windows PowerShell are comparable.
The interesting part is that the messy multiplication is faster than using the Math function – something to remember if speed of calculation becomes an issue in future code.