c# - get time of accuracy of nanoseconds

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

  }
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值