0

我尝试计算操作之间的时间。所以,用相同的代码编写两个方法,但使用不同的方式。起初我喜欢这样:

private static void calcAverageTimeUid(ISomeObject someObj, int N,ISnapshot _Snapshot)
    {
        Stopwatch stopWatch = new Stopwatch();
        int averageTime = 0;
        var uid = someObj.Uid;

        for (int i = 0; i < N; i++)
        {
            stopWatch.Start();
            var coll = _Snapshot.GetObject(uid);
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;
            averageTime = averageTime + ts.Milliseconds;
        }
        averageTime = averageTime / N;

    }

我有结果平均时间,例如 500 毫秒。N=1000000 甚至更多。

但是,我将此方法重写为两种方法:mainCalc,其中应该包含其他方法,fe 以获取 uid、id、name 等的平均时间。

主计算:

private static void mainCalc(ISomeObject someObj,int N,ISnapshot _Snapshot)
    {

        int averageTimeUID = 0;
        for (int i = 0; i < N; i++)
        {
            var tmp=calcAverageTimeUid2(someObj,N,_Snapshot);
            averageTimeUID+=tmp;
        }
        averageTimeUID = averageTimeUID / N;
    }

和其他方法:

private static int calcAverageTimeUid2(ISomeObject someObj,int N,ISnapshot _Snapshot)
    {
        Stopwatch stopWatch = new Stopwatch();
        var prop = someObj.Uid;
        stopWatch.Start();
        var obj = _Snapshot.GetObject(prop);
        stopWatch.Stop();
        TimeSpan ts = stopWatch.Elapsed;
        return ts.Milliseconds;
    }

所以,我在这个方法中运行 mainCalc 并运行 calcAcerageTimeUid2。而 stopWatch=0 毫秒的结果!

这是错误的结果还是不是?我不明白 - 什么方式使用秒表对吗?

PS删除多余的秒表之一。

PPS 谢谢大家!

4

3 回答 3

2

你的第一个例程应该是

    for (int i = 0; i < N; i++)
    {
        stopWatch.Start();
        var coll = _Snapshot.GetObject(uid);
        stopWatch.Stop();
    }
    averageTime = stopWatch.Elapsed / N;

注意,stopWatch.Start()不会秒表重置为零。

于 2013-10-25T11:52:32.327 回答
1

The reason you get different results is because you are rounding the number of milliseconds in different places. In your first method, you use one stopwatch and continuously Start() and Stop() it. Your operation must take less than 1 ms, but when you repeatedly start and stop the same stopwatch, the total number of ticks will still increase. That is why with N=1000000 you got only 500 ms.

In the second method, you start and stop a new stopwatch each time, and return the milliseconds. Since each operation is averaging 500/1000000 = 0.00005 ms, the ticks of the stopwatch will accumulate some small value, but the ElapsedMilliseconds (or Milliseconds of the TimeSpan) will still be 0.

EDIT: To solve your problems, the first loop should use the final Elapsed value of the stopwatch once the loop is complete (like the 2nd example in sgmoore's answer). The second method should return the ticks from the method rather than milliseconds and then calculate the milliseconds from the tick frequency of the stopwatch.

In summary, the first operation you are summing a bunch of values like 0.00005, in the second you are summing a bunch of 0s.

于 2013-10-25T11:52:53.833 回答
1

Milliseconds is not TotalMilliseconds.

Milliseconds is the entire number of milliseconds of the TimeSpan. Not the total milliseconds which is a double, so you are losing the precision under 1ms.

And why do you return an int, instead of the TimeSpan?

Try this code :

private static void mainCalc(ISomeObject someObj, int N, ISnapshot _Snapshot)
{
    var averageTimeUID = TimeSpan.Zero;
    for (int i = 0; i < N; i++)
    {
        averageTimeUID += calcAverageTimeUid2(someObj,N,_Snapshot);
    }
    averageTimeUID = new TimeSpan( averageTimeUID.Ticks / N );
}

The other method:

private static TimeSpan calcAverageTimeUid2(ISomeObject someObj, int N, ISnapshot _Snapshot)
{ 
    var stopWatch = new Stopwatch();
    var prop = someObj.Uid;
    stopWatch.Start();
    var obj = _Snapshot.GetObject(prop);
    stopWatch.Stop();
    return stopWatch.Elapsed;
}
于 2013-10-25T12:01:57.663 回答