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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay