PowerShell for Windows Admins

Jan 29 2020   12:39PM GMT

Time for cube calculation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


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.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: