How to check elapsed milliseconds using Thread.Sleep in for loop with C#

1 Answer

0 votes
using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void testSpeed(int ms) {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();

        for (int i = 0; i < 1000; i++) {
            Thread.Sleep(ms); // Sleeps for ms millisecond
        }

        stopwatch.Stop();
        Console.WriteLine($"Executed in {stopwatch.ElapsedMilliseconds} ms");
    }

    static void Main()
    {
        testSpeed(1);
    }
}


/*
run:

Executed in 1076 ms

*/

 



answered Jul 9, 2025 by avibootz

Related questions

1 answer 207 views
1 answer 168 views
1 answer 114 views
114 views asked Sep 1, 2024 by avibootz
1 answer 98 views
98 views asked Sep 1, 2024 by avibootz
...