DEV Community

Cover image for Measuring program execution time in PowerShell
Bruno Baère
Bruno Baère

Posted on • Edited on

5 2

Measuring program execution time in PowerShell

As I was nearing completion of implementing Ray Tracing in One Weekend in Go, I felt the need to measure the time it would take to render a scene1.

My day-to-day work is in CentOS, so I was tempted to try time gortiw in Windows Terminal, as it already had some improvements over cmd, like ssh and an alias for ls. I was disappointed. There was no easily accessible time-like command nor it was an alias.

After searching a little, I got to a solution: using PowerShell's Measure-Command. Then I just needed to create a function time in my PowerShell profile and it was done.

Steps

  1. Edit your Documents\PowerShell\Microsoft.PowerShell_profile.ps1
  2. Add the line

    function time { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-default} }
    
  3. Open a new instance or tab of PowerShell to use the new time function.
    Ex: time gortiw.exe

Satisfied with the results, the next step will be using a profiler to check where to improve.

Notes

1. Yes, I could have used a timer in Go inside the ray tracer itself, but having a timer like `time` is still useful.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more