What is the equivalent to System.nanoTime() in .NET?

前端 未结 7 1430
暖寄归人
暖寄归人 2021-01-07 20:22

The title is pretty much self-explanatory, I\'m killing myself over this simplicity.

Looked here, but it isn\'t much helpful.

相关标签:
7条回答
  • 2021-01-07 20:53

    If you want a timestamp to be compared between different processes, different languages (Java, C, C#), under GNU/Linux and Windows (Seven at least):

    Java:

    java.lang.System.nanoTime();
    

    C GNU/Linux:

    static int64_t hpms_nano() {
       struct timespec t;
       clock_gettime( CLOCK_MONOTONIC, &t );
       int64_t nano = t.tv_sec;
       nano *= 1000;
       nano *= 1000;
       nano *= 1000;
       nano += t.tv_nsec;
       return nano;
    }
    

    C Windows:

    static int64_t hpms_nano() {
       static LARGE_INTEGER ticksPerSecond;
       if( ticksPerSecond.QuadPart == 0 ) {
          QueryPerformanceFrequency( &ticksPerSecond );
       }
       LARGE_INTEGER ticks;
       QueryPerformanceCounter( &ticks );
       uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
       nano *= 100UL;
       return nano;
    }
    

    C#:

    private static long nanoTime() {
       long nano = 10000L * Stopwatch.GetTimestamp();
       nano /= TimeSpan.TicksPerMillisecond;
       nano *= 100L;
       return nano;
    }
    
    0 讨论(0)
提交回复
热议问题