Question

I try to calculate time between operations. So,write two methods with equal code ,but use different ways. At first way i do like that:

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;

    }

And i have result averageTime such as 500 milliseconds. N=1000000 and more.

But , i rewrite this method to two methods: mainCalc ,wich should contains come other methods,f.e. to get average time of uid,id,name and so on.

mainCalc:

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;
    }

And other method:

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;
    }

So, i run mainCalc and run calcAcerageTimeUid2 in this method.And result of stopWatch=0 milliseconds!

It is wrong result or not? I dont understand-what way of use stopWatch right?

P.S. delete one of excess StopWatch.

P.P.S. Thank all of you!

Était-ce utile?

La solution 2

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;
}

Autres conseils

Your first routine should be

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

Note, stopWatch.Start() does not reset the stopwatch back to zero.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top