# Anyone know why this timer is off?

**URL:** https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688
**Category:** PowerShell Help
**Created:** [February 19, 2023, 1:59am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688 "2023-02-19T01:59:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)
#### Post date: [February 19, 2023, 1:59am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/1 "2023-02-19T01:59:26Z")

</div>

Hello,  
Can anyone say why this timer script is off a bit?

```auto
$timer = New-Object System.Diagnostics.Stopwatch
$seconds -eq 0
do
{
$seconds = ($timer.elapsed.totalseconds)
$timer.start()
$seconds
Start-Sleep -Milliseconds 1000
}
until ($timer.elapsed.minutes -eq 59)
$timer.elapsed.minutes

```

Output:

```auto
False
0
1.0118032
2.0162498
3.0236591
4.029671
5.0310328
6.0408992
7.0432709

```

After ~130 seconds it skips a second due to the timing

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [February 19, 2023, 2:13am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/2 "2023-02-19T02:13:12Z")

</div>

> [@matt1974](#):
>
> Can anyone say why this timer script is off a bit?

What exactly do you mean? What do you expect?

> [@matt1974](#):
>
> After ~130 seconds it skips a second due to the timing

Do you mean this:

```auto
101,9591559
102,9737869
 103,9898191
 105,0034982
106,0189042
107,0333377

```

?

It does not skip one second. Your loop takes a little bit more than a second. And when the loop iteration starts “_at the end_” of a second it will need until “_the start_” of the second after the next second. … so from `103,9898191` to `105,0034982`.

Or did I get everything wrong? 🤔

---

<div class="post-metadata">

### Author: ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)
#### Post date: [February 19, 2023, 2:17am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/3 "2023-02-19T02:17:23Z")

</div>

Well, after I do $timer.start(), it starts at 1.0118032.  
Then sleeps 1 second, and output is 2.0162498.

Thats more than 1 second?

---

<div class="post-metadata">

### Author: ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)
#### Post date: [February 19, 2023, 2:19am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/4 "2023-02-19T02:19:43Z")

</div>

1 other thing…  
When I run it 2 times in a row, I get slightly different output. Why is that?

False  
0  
1.0092845  
2.0147815  
3.0197994

False  
0  
1.0140531  
2.0195799  
3.0221682

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [February 19, 2023, 2:25am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/5 "2023-02-19T02:25:43Z")

</div>

> [@matt1974](#):
>
> it starts at 1.0118032.  
> Then sleeps 1 second, and output is 2.0162498.
> 
> Thats more than 1 second?

Are you really asking if `2.0162498` minus `1.0118032` is greater than `1`? 🤨 you can do the math … 😉

> [@matt1974](#):
>
> Why is that?

Because this world is non-deterministic and there is very likely more running on your computer than just the PowerShell session. 🤷🏽‍♂️

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [February 19, 2023, 9:49am UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/8 "2023-02-19T09:49:10Z")

</div>

What is actually your issue? Do you try to solve a particular problem? 🤔

PowerShell and Windows in general are not supposed to be used for precise time measurements. Windows is not a real time OS. And you may consider that it takes time to run code - even if it’s just a small amount.  
Doing a `Start-Sleep -Milliseconds 1000` will probably take almost exactly 1 second. But doing a `Write-Host` as the next command in your loop will already take some more milliseconds. So if you run this a thousand times of course it will be more than 1000 seconds. 🤷🏽‍♂️ Is that really so suprising for you? You may run a loop with only a `Start-Sleep` in it and then a loop togehter with a `Write-Host` in it and compare them. 👆🏽😉

---

<div class="post-metadata">

### Author: ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)
#### Post date: [February 20, 2023, 3:42pm UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/9 "2023-02-20T15:42:10Z")

</div>

Complete agreement with Olaf. I would also point out that every line of code takes a small slice of time to execute so after you have slept for 1 second you have 4 other lines of code that need to execute before you see the next increase. Those take time and honestly outputting to a screen is slow in comparison to how fast a computer operates.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:15pm UTC](https://forums.powershell.org/t/anyone-know-why-this-timer-is-off/21688/10 "2024-05-16T20:15:07Z")

</div>


