C#C
C#2y ago
TiEmKej

✅ Time of method differs between Stopwatch.GetTimestamp and DateTime.Now

Hi there, I have to codes, that differs only in the way that they are recording time of the same operation. It there a big deal that they are once use DataTime and TimeSpan and other Stopwatch.GetTimeStamp?
static void LMaxT()
{
    DateTime startTime, stopTime;
    TimeSpan totalTime = TimeSpan.Zero;
    for (int i = 0; i < NumOfIter ; i++)
    {
        startTime = DateTime.Now;
        bool Present = IsPresent_Linear(sourceTable, sourceTable.Length - 1);
        stopTime = DateTime.Now;

        totalTime += stopTime - startTime;
    }
    TimeSpan lenghtOfCalc = totalTime / NumOfIter;
    Console.Write($"\t{lenghtOfCalc.TotalSeconds.ToString("F4")}s");
}

static void LinearMaxTim()
{
    double ElapsedSeconds;
    long ElapsedTime = 0, MinTime = long.MaxValue, MaxTime = long.MinValue, IterationElapsedTime;
    for (int n = 0; n < (NumOfIter); ++n)
    {
        long StartingTime = Stopwatch.GetTimestamp();
        bool Present = IsPresent_Linear(sourceTable, sourceTable.Length - 1);
        long EndingTime = Stopwatch.GetTimestamp();
        IterationElapsedTime = EndingTime - StartingTime;
        ElapsedTime += IterationElapsedTime;
        if (IterationElapsedTime < MinTime) MinTime = IterationElapsedTime;
        if (IterationElapsedTime > MaxTime) MaxTime = IterationElapsedTime;
    }
    ElapsedTime -= (MinTime + MaxTime);
    ElapsedSeconds = ElapsedTime * (1.0 / (NumOfIter * Stopwatch.Frequency));
    Console.Write("\t" + ElapsedSeconds.ToString("F4")+"s");
}
image.png
Was this page helpful?