C
C#9mo 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");
}
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");
}
No description
9 Replies
Angius
Angius9mo ago
Stopwatch will, in general, be the most accurate I believe
TiEmKej
TiEmKejOP9mo ago
Damn, this inaccuracy is big af If it's it
Angius
Angius9mo ago
If you want to benchmark execution time of something, though Benchmark.NET will be the most accurate
TiEmKej
TiEmKejOP9mo ago
Nah, it's just a simple project for University The Prof gave sample with Timestamp, and i was curious why DataTime had this much longer "execution" time
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Petris
Petris9mo ago
you can also do
long start = Stopwatch.GetTimestamp();
// do something
return Stopwatch.GetElapsedTime(start);
long start = Stopwatch.GetTimestamp();
// do something
return Stopwatch.GetElapsedTime(start);
and you need no objects for it
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton429mo ago
TIL that method added .NET 7 neat
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server