why the results of Benchmark and StopWatch so different?

76 Views Asked by At

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++)
        {
            
        }
    }
}

enter image description here

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?

0

There are 0 best solutions below