© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
39 replies
Deleted User 07v456ee0

Stopwatch not giving accurate times

hey everyone, im using stopwatch to measure total milliseconds, but the timing is way off. for example, when
increment
increment
is 50k for this piece of code, i count 30 seconds irl but the console says 0.1105ms, which is not even close to correct
            List<float> uLSexI = new();
            tools.log("Unsorted Linear Search, EXISTING Item", ConsoleColor.Red);
            foreach (int increment in increments)
            {
                tools.log($"Increment {increment}");
                genItems(increment);
                item[] itemsUnsorted = items.ToArray();
                int existingQ = itemsUnsorted[random(0, itemsUnsorted.Length)].quantity;
                long startTime = Stopwatch.GetTimestamp();
                int indexFound = LinearSearch(itemsUnsorted, existingQ);
                TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime);
                tools.log($"Increment {increment} complete in {(float)elapsedTime.TotalMilliseconds}ms ({elapsedTime.TotalSeconds}s)");
                uLSexI.Add((float)elapsedTime.TotalMilliseconds);
            }
            List<float> uLSexI = new();
            tools.log("Unsorted Linear Search, EXISTING Item", ConsoleColor.Red);
            foreach (int increment in increments)
            {
                tools.log($"Increment {increment}");
                genItems(increment);
                item[] itemsUnsorted = items.ToArray();
                int existingQ = itemsUnsorted[random(0, itemsUnsorted.Length)].quantity;
                long startTime = Stopwatch.GetTimestamp();
                int indexFound = LinearSearch(itemsUnsorted, existingQ);
                TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime);
                tools.log($"Increment {increment} complete in {(float)elapsedTime.TotalMilliseconds}ms ({elapsedTime.TotalSeconds}s)");
                uLSexI.Add((float)elapsedTime.TotalMilliseconds);
            }
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Vost-api not giving accurate results
C#CC# / help
15mo ago
❔ Difference between Dispatchertimer and Stopwatch?
C#CC# / help
3y ago
Tested it a lot of times but still giving unexpected error
C#CC# / help
4y ago
❔ First stopwatch taking longer for same function
C#CC# / help
3y ago