精準時間 + nanoSleep(...)

精準時間 + nanoSleep(...)


int gettimeofday (struct timeval *tv, void* tz)
{
  union {
    __int64 ns100; /*time since 1 Jan 1601 in 100ns units */
    FILETIME ft;
  } now;


  GetSystemTimeAsFileTime (&now.ft);
  tv->tv_usec = (long) ((now.ns100 / 10L) % 1000000L);
  tv->tv_sec = (long) ((now.ns100 - 116444736000000000L) / 10000000L);
  return (0);
}

typedef  struct _BinInt32
{
    __int32 i32[2];
} BigInt32;

typedef  struct _BigInt64
{
    __int64 i64;
} BigInt64;

typedef union _bigInt
{
    BigInt32 int32val;
    BigInt64 int64val;
    LONGLONG dataBuf;
} BigInt;

unsigned int getRandomSeed(void)
{
BigInt timeCounter;

    QueryPerformanceCounter((LARGE_INTEGER*)&timeCounter);
    return((unsigned int)timeCounter.dataBuf);
}

void nanoSleepEx(int delay)
{
BigInt start_ticks, end_ticks, cpuFreq;
double dfFreq;
LONGLONG dfCount;

    QueryPerformanceFrequency((LARGE_INTEGER*)&cpuFreq);
    dfFreq = ((double)cpuFreq.dataBuf) / 1000000000L;
    dfCount = (LONGLONG)(((double)delay) * dfFreq);

    _asm
    {
        RDTSC
        mov start_ticks.int32val.i32[0], eax
        mov start_ticks.int32val.i32[4], edx
    }

    do
    {
        _asm
        {
            RDTSC
            mov end_ticks.int32val.i32[0], eax
            mov end_ticks.int32val.i32[4], edx
        }

    } while((end_ticks.dataBuf - start_ticks.dataBuf - dfCount) < 0);
}

你可能感兴趣的:(精準時間 + nanoSleep(...))