`

c# - get time of accuracy of nanoseconds

    博客分类:
  • C#
c# 
阅读更多

To get time of nanoseconds accuracy, what you can do inside C# is to use the Stopwatch classes from the System.Stopwatch class. 

 

 

 

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

  }
 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics