C# - Thread.Sleep(1); takes more than 1 milisecond - around 16ms

I recently had to hunt down a bug. We were experiencing a piece of code running very slowly. The executed code should take way less than a millisecond but instead it took about 15-16ms. What I discovered when looking into the codebase was a simple Thread.Sleep(1);. This is usually a smell but 1 millisecond could not make up 16ms right? There I was wrong.

A quick look around the internet and I found several articles that states that Thread.Sleep(1); takes up 15.6ms. As this is the lowest a thread.sleep call takes (when using defaults). This is due to Thread.Sleep being affected by windows clock interrupt rate. The default is 64 times per second and (1000/64) is 15.625ms.

If you only want to wait for 1ms you can always use a stopwatch. However this is not making the thread sleep - it is blocking. Below is an example:

var sw = Stopwatch.StartNew();
while (sw.ElapsedTicks < 10000) {}
//TODO - do stuff

I hope this helps you, let me know in the comments if it did or did not!