C# time in microseconds

后端 未结 4 1347
一个人的身影
一个人的身影 2020-11-30 05:18

I am searching how to format time including microseconds. I\'m using class DateTime, it allowes (using properties) to get data till miliseconds, which is not enougth. I trie

4条回答
  •  旧巷少年郎
    2020-11-30 05:53

    I was unable to get Johns tick to micorosecond conversion to work. Here is how I was able to measure Microsecond and Nanosecond resolution by using ticks and the Stopwatch:

    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    // Do something you want to time
    
    sw.Stop();
    
    long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L*1000L));
    long nanoseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L*1000L*1000L));
    
    Console.WriteLine("Operation completed in: " + microseconds + " (us)");
    Console.WriteLine("Operation completed in: " + nanoseconds + " (ns)");
    

提交回复
热议问题