PowerShell/Community-Blog

Update request-How long does it take to run a script

doctordns opened this issue · 7 comments

I often see queries about low long it takes for a script or script block to run.

Summary of the update request

Cover subtracting two DateTime objects to get duration
Also should cover Measure-command

Cover Get-Date and timespan objects.

Hi,
Is this post available for pickup?

Yes - please consider another post!!

Kagre commented

also worth noting that .ticks will sometimes not increment

PS> $x=[datetime]::now.ticks; 1..100 | %{$x - ($x = [datetime]::now.ticks)}
-155907
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-156216
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-156204
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-156220
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-156207
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Kagre commented

This one seems more believable

PS> $sw = [System.Diagnostics.Stopwatch]::new()
PS> $x=0;$sw.reset();$sw.start(); 1..100 | %{$x - ($x = $sw.elapsed.ticks)};$sw.Stop();$sw.Elapsed
-153047
-17844
-11818
-9142
-4155
-4756
-4866
-5006
-4283
-3974
-3908
-3977
-3991
-4288
-3876
-4220
-4261
-4337
-4064
-3929
-3906
-3874
-4146
-4466
-4826
-4297
-4030
-4077
-4019
-4250
-4015
-3961
-4091
-4138
-4407
-4275
-4326
-10629
-4543
-4231
-4922
-4064
-3987
-4378
-36011
-11488
-8482
-4686
-4490
-4015
-6342
-5634
-4206
-3923
-4215
-3906
-3923
-4012
-3847
-4044
-3909
-3757
-4400
-4100
-3991
-4034
-4207
-3888
-4111
-22495
-4261
-12093
-4069
-4003
-3833
-4281
-3959
-4160
-3980
-3959
-4100
-3846
-3718
-3980
-5749
-5584
-4556
-3966
-4216
-4302
-4282
-3965
-3823
-3966
-4137
-4063
-4182
-3985
-3933
-4058

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 68
Ticks             : 680952
TotalDays         : 7.88138888888889E-07
TotalHours        : 1.89153333333333E-05
TotalMinutes      : 0.00113492
TotalSeconds      : 0.0680952
TotalMilliseconds : 68.0952

Is this post already taken? If not I can work on it.

Is this post already taken? If not I can work on it.

This is open for anyone. I would show how to use Measure-Command. There are several examples in https://learn.microsoft.com/powershell/scripting/dev-cross-plat/performance/script-authoring-considerations

Definitely, and maybe an extra function to get the time it took plus the execution result.
I'll start working on it.