.net StopWatch 的计时不一致

inconsistent timing from .net StopWatch

我有一个使用 GPU (NVIDA GTX980) 进行图像处理的 C# 和 .NET 应用程序。有 4 个阶段,我将 CPU 同步到 GPU(时间上没有重叠)以进行计时。但这些数字并没有加起来。

Launch() will do a async launch of the GPU kernel) but synchronize() will wait till it is done.

  1. 总计: t阈值:4.2827ms

这是怎么回事?

   public static void threshold()
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();
        gpu.Lock();
        dim3 block = new dim3(tileWidthBig, tileHeightBig);
        dim3 grid = new dim3(Frame.width / tileWidthBig, Frame.height / tileHeightBig);
        gpu.Launch(grid, block).gHistogram(gForeground, gPercentile, gInfo);
        gpu.Synchronize();
        tHistogram = watch.Elapsed.TotalMilliseconds;

        block = new dim3(1024);
        grid = new dim3(1);
        gpu.Launch(grid, block).gSumHistogram(gPercentile);
        gpu.Synchronize();
        tHistogramSum = watch.Elapsed.TotalMilliseconds - tHistogram;

        gpu.Launch(grid, block).gIQR(gPercentile, gInfo);
        gpu.Synchronize();
        tIQR = watch.Elapsed.TotalMilliseconds - tHistogramSum;

        block = new dim3(256, 4);
        grid = new dim3(Frame.width / 256, Frame.height / 4);
        gpu.Launch(grid, block).gThreshold(gForeground, gMask, gInfo);
        gpu.Synchronize();
        tThresholdOnly = watch.Elapsed.TotalMilliseconds - tIQR;

        gpu.Unlock();
        watch.Stop();
        tThreshold = watch.Elapsed.TotalMilliseconds;
    }

由于 TotalMilliseconds 不断递增并且您试图找出时间点之间的差异,因此您需要在第二个差异之后减去前面差异的总和,因此:

tIQR = watch.Elapsed.TotalMillisconds - (tHistogram + tHistogramSum);

&

tThresholdOnly= watch.Elapsed.TotalMillisconds - (tHistogram + tHistogramSum + tIQR);