Stopwatch not giving accurate times

hey everyone, im using stopwatch to measure total milliseconds, but the timing is way off. for example, when 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);
}
22 Replies
ero
ero2y ago
what is Stopwatch exactly? the methods GetTimestamp and GetElapsedTime don't exist on System.Diagnostics.Stopwatch
Deleted User 07v456ee0
yeah they do
Deleted User 07v456ee0
im just using the Stopwatch from system.diagnostics
ero
ero2y ago
ah, they're static methods yeah so when you do these 3 lines
long startTime = Stopwatch.GetTimestamp();
int indexFound = LinearSearch(itemsUnsorted, existingQ);
TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime);
long startTime = Stopwatch.GetTimestamp();
int indexFound = LinearSearch(itemsUnsorted, existingQ);
TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime);
then you literally measured the time from long startTime = Stopwatch.GetTimestamp(); to TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime); which is basically non-existent actually, nevermind, not thinking well today it seems are you sure LinearSearch takes that long? and it's not something else? what do you actually want to measure here?
Saber
Saber2y ago
if genItems is what takes time, the start time should be defined before there
ero
ero2y ago
a normal stopwatch instance would make this easier stopwatches are also inaccurate in general and should not be used to benchmark code
Deleted User 07v456ee0
im measuring linear search here, and even if i wasn't genItems doesn't take long (i know this from before manual timing) i was using one lol but i had the same issue so I tried this and it didn't work either what should I use instead because I need to measure the amount of time it takes for some data collection
ero
ero2y ago
can you show more context?
Deleted User 07v456ee0
and doing it manually is not possible beacause with a small number of items (less than 1000 for example) its almost instant
ero
ero2y ago
we can't really tell you why LinearSearch is so fast if we don't know what it looks like
Deleted User 07v456ee0
static int LinearSearch(item[] array, int target)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i].quantity == target)
{
return i;
}
}
return -1;
}
static int LinearSearch(item[] array, int target)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i].quantity == target)
{
return i;
}
}
return -1;
}
its just a simple for loop
ero
ero2y ago
to benchmark code, you want to use a dedicated library like benchmarkdotnet $benchmark
MODiX
MODiX2y ago
Try BenchmarkDotNet if you want a sophisticated library for benchmarking https://github.com/dotnet/BenchmarkDotNet
Deleted User 07v456ee0
okay ill take a look thanks
ero
ero2y ago
i mean, is the item to find potentially very early in the array? if it's found instantly, then of course it's gonna take no time at all
Saber
Saber2y ago
even with an array of 50k items, theres no way that method would take very long
ero
ero2y ago
(also that)
MODiX
MODiX2y ago
just_ero#0000
REPL Result: Success
int[] ints = Enumerable.Range(0, 50000).ToArray();
Stopwatch sw = Stopwatch.StartNew();

for (int i = 0; i < ints.Length; i++)
{
if (i == 49999)
break;
}

sw.Stop();
sw.Elapsed
int[] ints = Enumerable.Range(0, 50000).ToArray();
Stopwatch sw = Stopwatch.StartNew();

for (int i = 0; i < ints.Length; i++)
{
if (i == 49999)
break;
}

sw.Stop();
sw.Elapsed
Result: TimeSpan
00:00:00.0011020
00:00:00.0011020
Compile: 548.432ms | Execution: 49.352ms | React with ❌ to remove this embed.
ero
ero2y ago
that's 1.1ms
Deleted User 07v456ee0
i tried it on 30k and counted the seconds, it was 3, and the code gave out 0.0278 ms
ero
ero2y ago
then it isn't LinearSearch that takes that long
Deleted User 07v456ee0
im gonna try moving the timestamp turns out it was genItems lol, a small change I made made it take a lot longer thanks for the help 👍
Want results from more Discord servers?
Add your server