i want to measure a method's performance and use the two different way, but the results is confused me.
Method 1:
new Test().Main();
public class Test
{
public void Main()
{
long a = Stopwatch.GetTimestamp();
try
{
for (int i = 0; i < 1000000; i++)
{
}
}
finally
{
long b = Stopwatch.GetTimestamp();
Console.WriteLine(Stopwatch.GetElapsedTime(a,b)); // output:00:00:00.0008388 = 8388us
}
}
}
Method 2:
BenchmarkRunner.Run<Test>();
[MemoryDiagnoser]
public class Test
{
[Benchmark]
public void Main1()
{
for (int i = 0; i < 1000000; i++)
{
}
}
}
the measure ways i used is right? if that's right, why the results is so different?(8388us vs 258us) and which result should i rely for measure performance?