Calculating time by the C++ code

后端 未结 7 845
囚心锁ツ
囚心锁ツ 2021-01-02 22:21

I know this question has been asked few times over SO but none of them is really helping me out, so asking again.

I am using windows xp and running visual studio c++

7条回答
  •  庸人自扰
    2021-01-02 23:02

    If you're on Windows, the GetTickCount() function is a handy way to get a timer with more resolution than 1 second. The resolution of GetTickCount depends on your operating system, but it's probably somewhere between 10 ms and 16 ms.

    Note that for quick operations, doing your code once won't be enough. Your code might run in like 0.02 ms, which means you'll get 0 from a counter such as GetTickCount. Your code may not execute for long enough for the timer to "tick" over to the next value. The solution is to run your code in a loop a million times or whatever, time the whole lot, then divide by a million.

提交回复
热议问题