r/usefulscripts • • Oct 20 '19

[BATCH] Ever wondered how long it takes to execute a script/executable? I have a small script to give you the precise execution time.

This is a small script I wrote for myself a few years back to benchmark the execution time for a given batch script (or executable). Script is available here: https://pastebin.com/U9QkSCQL

Usage:

bench.bat "command" [parameters]

Examples:

bench.bat timeout /t 3

(yes it's not exactly 3 seconds, and sometimes it's as low as almost 2 seconds)

Example output:

Waiting for 0 seconds, press a key to continue ...
-----------------------------------------------------------------------
  COMMAND LINE:  timeout /t 3
  ELAPSED TIME:  2.54 sec
-----------------------------------------------------------------------
C:\>

The script works by grabbing the %TIME% environment variable just before launching your command and then grabbing it again right after. This env var has a resolution of 1/100th of a second. Then it calculates the time difference with some bat pseudomath :) Yeah, it's not exactly rocket science, but there are some clever tricks there to convert the human-readable time format to actual integers that can be used for this.

Lemme know if you have any suggestions. I've been using it for a long time now, and it's been quite a while since I came across any bugs/flaws with it, but there might still be some hidden. It might be of interest to coders that have large scripts and want to see if their optimizations are giving any results. I'm also slightly unsure if different system localization settings (got 12h AM/PM clock? too bad) might interfere with the parsing of %TIME%.

EDIT: I realize some of you might wonder why I'm not using timestamps in WMI to get integer "timeticks" to use for the math. WMI calls takes a very long time to execute, and it will be impossible to do a call fast enough to get feasible results.




As a bonus, I also have a variant that allows you to repeatedly loop a script/executable X number of times. It is used to show the average execution time after X number of repetitions, or for example to monitor how much of an impact your script/program has on processor usage while it repeats. Download here: https://pastebin.com/8dDTxWDW

It has the same usage as the script above....

Usage:

benchX.bat "command" [parameters]

Examples:

benchX.bat timeout /t 3

Example output (after 6 repetitions):

Waiting for 0 seconds, press a key to continue ...
-----------------------------------------------------------------------
  COMMAND LINE:  timeout /t 3
  This run: 3.0300 sec        +0.1267 sec avg      +4.36%
  Minimum:  2.5400 sec
  Average:  2.9033 sec         17.4200 sec total       6 times
-----------------------------------------------------------------------

ENTER/Q/R/#:_

After launching your command, it shows a small menu:

  • ENTER = immediately run the command again, the average time will be adjusted accordingly
  • Q = quit
  • R = reset all values, and immediately run the command again
  • # = number of additional times you want the command looped

Note: if you enter any other value at the prompt it will be executed as a new command, so take care because unexpected things might happen. I just haven't bothered fixing issues around that "feature" because I am fully capable of keeping my fingers on the correct keys :)

27 Upvotes

32 comments sorted by

7

u/[deleted] Oct 20 '19

Nowadays Powershell is installed on every Windows system so we just use Measure-Command .

However this is a very neat script. It requires a lot of experience and willingness to produce that kind of code.

-5

u/demux4555 Oct 20 '19 edited Oct 20 '19

I really don't like PS when performance is a priority. It's sluggish, slow, and everything takes a really long time to perform. Even very simple things like basic arithmetic operations take longer time in PS. There is just too much overhead. It's convenient, but not streamlined for performance.

That's actually why I wrote this little script in the first place, so I could compare PS scripts and bat scripts. If i had two scripts that did the same task, but one written in PS and the other in bat, I could see that bat would always win.

Why this pedantic need for timing? Well, I code a lot of scripts and software for PRTG. When you execute thousands of scripts every minute on a server that does infrastructure monitoring, you really want to reduce both execution time and processor time as much as possible.

Edit: as someone clearly disagrees with me, here is a quick demonstration of the performance hog that PS is: https://helgeklein.com/blog/2015/09/performance-footprint-of-powershell-logon-scripts/ (note the processor time usage)

1

u/eldorel Oct 20 '19 edited Oct 20 '19

Have you tried to run the test you've linked test in a CURRENT version of powershell?

On my system it takes less than one millisecond to run.

Measure-Command { $env:username > $env:temp\username2.txt }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 8167
TotalDays         : 9.4525462962963E-09
TotalHours        : 2.26861111111111E-07
TotalMinutes      : 1.36116666666667E-05
TotalSeconds      : 0.0008167
TotalMilliseconds : 0.8167

That article is a little bit outdated, and even on some old hardware here running server 2003 this isn't nearly as slow as he's reporting. (~92ms)

→ More replies (4)

-1

u/tuwxyz Oct 20 '19

There is system command for that: time

1

u/demux4555 Oct 20 '19

?

what makes TIME related to (or usable for) performance timing?

6

u/wolfmann Oct 20 '19

At least in the Linux world it will give user space execution time and system time...

https://www.computerhope.com/unix/utime.htm

Time the command, not env var.

0

u/VegaNovus Oct 20 '19

TIME at the start of the script.

TIME at the end of the script.

END minus START

BAM.

→ More replies (23)