c# - get time of accuracy of nanoseconds
4543 단어 C#
below is the code that shows how to program on nanoseconds .
public static long NanoTime
{
get { return (long)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000000000.0)); }
}
There is a twiki on the nanosecond with the help of stopwatch. - How do you convert Stopwatch to nanoseconds, milliseconds and seconds...
While another possibly use of high definition of timer is through the DateTime class, the DateTime class does not provide the accuracy of Sotpwatch, but you can still get very high accurate of milli-seconds value.
The fact with DateTime is that DateTime has a Ticks propery, while the resolution of the DateTime.Ticks is known to be 100 nanoseconds. Also , we now Timespan has some readonly fields which is able to telll how many ticks are there in one milliseconds and how mnay ticks are there in one nanoseconds.
Is there a high resolution (microsecond, nanosecond) DateTime object available for the CLR?
There are some code that is taken from the discussion on that post.
/// <summary>
///
/// </summary>
/// <remarks>
/// The resolution of the DateTimer.Tick is 100 nanosecond, with the help of
///
/// </remarks>
public static class DateTimeExtensionMethods
{
/// <summary>
/// The number of ticks per microsecond.
/// </summary>
public const int TicksPerMicrosecond = 10;
/// <summary>
/// The number of ticks per Nanosecond.
/// </summary>
public const int NanosecondsPerTick = 100;
/// <summary>
/// Gets the microsecond fraction of a DateTime.
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static int Microseconds(this DateTime self)
{
return (int)Math.Floor((self.Ticks % TimeSpan.TicksPerMillisecond) / (double)TicksPerMicrosecond);
}
/// <summary>
/// Gets the Nanosecond fraction of a DateTime. Note that the DateTime
/// object can only store nanoseconds at resolution of 100 nanoseconds.
/// </summary>
/// <param name="self">The DateTime object.</param>
/// <returns>the number of Nanoseconds.</returns>
public static int Nanoseconds(this DateTime self)
{
return (int)(self.Ticks % TimeSpan.TicksPerMillisecond % TicksPerMicrosecond) * NanosecondsPerTick;
}
/// <summary>
/// Adds a number of microseconds to this DateTime object.
/// </summary>
/// <param name="self">The DateTime object.</param>
/// <param name="microseconds">The number of milliseconds to add.</param>
public static DateTime AddMicroseconds(this DateTime self, int microseconds)
{
return self.AddTicks(microseconds * TicksPerMicrosecond);
}
/// <summary>
/// Adds a number of nanoseconds to this DateTime object. Note: this
/// object only stores nanoseconds of resolutions of 100 seconds.
/// Any nanoseconds passed in lower than that will be rounded using
/// the default rounding algorithm in Math.Round().
/// </summary>
/// <param name="self">The DateTime object.</param>
/// <param name="nanoseconds">The number of nanoseconds to add.</param>
public static DateTime AddNanoseconds(this DateTime self, int nanoSeconds)
{
return self.AddTicks((int)Math.Round(nanoSeconds / (double)NanosecondsPerTick));
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.