What should I use to replace gettimeofday() on Windows?

后端 未结 4 1409
半阙折子戏
半阙折子戏 2020-12-13 05:14

I\'m writing a portable Socket class that supports timeouts for both sending and receiving... To implement these timeouts I\'m using select().... But, I sometim

4条回答
  •  自闭症患者
    2020-12-13 05:19

    How about:

    unsigned long start = GetTickCount();
    // stuff that needs to be timed
    unsigned long delta = GetTickCount() - start;
    

    GetTickCount() is not very precise, but will probably work well. If you see a lot of 0, 16 or 31 millisecond intervals, try timing over longer intervals or use a more precise function like timeGetTime.

    What I usually do is this:

    unsigned long deltastack;
    int samples = 0;
    float average;
    
    unsigned long start = GetTickCount();
    // stuff that needs to be timed
    unsigned long delta = GetTickCount() - start;
    
    deltastack += delta;
    if (samples++ == 10)
    {
       // total time divided by amount of samples
       average = (float)deltastack / 10.f;
       deltastack = 0;
       samples = 0;  
    }
    

提交回复
热议问题