How do you convert Stopwatch ticks to nanoseconds, milliseconds and seconds?

后端 未结 6 2003
悲哀的现实
悲哀的现实 2020-12-23 09:01

This is a very basic question...quite embarassing, but here goes:

I have a Stopwatch block in C# code. I determine the elapsed time in ticks and then want to conver

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 09:45

    According to MSDN, Frequency tells you the number of ticks per second. Therefore:

    Stopwatch sw = new Stopwatch();
    // ...
    double ticks = sw.ElapsedTicks;
    double seconds = ticks / Stopwatch.Frequency;
    double milliseconds = (ticks / Stopwatch.Frequency) * 1000;
    double nanoseconds = (ticks / Stopwatch.Frequency) * 1000000000;
    

提交回复
热议问题