Elapsed time unsigned tickCount / Wrapping

I have my own GetTickCount () function that returns unsigned int (counting the number to zero by 0xFFFFFFFF)

i cannot measure elapsed time with:

unsigned int elapsed; unsigned int start = GetTickCount(); LongOperation(); unsigned int stop = GetTickCount(); if (stop >= start ) elapsed = stop - start; else elapsed = (INT_MAX - start) + stop; 

is it the same if I am doing a cast to signing (the time interval is always less than what can be represented as an integer - I think about 24 days)?

 int start = (int)GetTickCount(); LongOperation(); int elapsedTime = (int)GetTickCount() - start; 

if I look at the .net Environmentmet.TickCount property:

TickCount will increase from zero to Int32 .. ::. MaxValue for about 24.9 days, then go to Int32 .. ::. MinValue, which is a negative number, and then increase to zero over the next 24.9 days.

so when I pass the GetTickCount () function to a signed integer, should I get the behavior from .net (the wrapper happens at 0x7FFFFFFF-> 0x80000000)?

with this it should be possible to measure elapsed time as it should (see in another post):

 int start = Environment.TickCount; DoLongRunningOperation(); int elapsedTime = Environment.TickCount - start; 
+2
source share
3 answers

The prototype of GetTickCount () in C ++ on Windows is: DWORD WINAPI GetTickCount (void);

So, I would encode it like this (similar to other answers):

 DWORD start = GetTickCount(); dosomething(); DWORD elapsed = GetTickCount() - start; 

Will measure elapsed time to the maximum number that DWORD can represent.

As others have said, with unsigned arithmetic, you don’t need to worry about flowing around the counter - try it yourself ...

Also check out GetTickCount64 and QueryPerformanceCounter / QueryPerformanceFrequency. GetTickCount64 allows you to measure longer intervals, but is not supported on all versions of Windows, while QueryPerformanceCounter allows you to measure much higher resolution and accuracy. For example, in some versions of windows GetTickCount () can only be accurate up to 18 ms, while QueryPerformanceCounter will be better than 1us.

+3
source

I'm not sure if GetTickCount () is the preferred function of your problem.

Can't you use QueryPerformanceFrequency ()? There is a good example at http://msdn.microsoft.com/en-us/library/ms644904%28v=VS.85%29.aspx

+1
source

In C ++, if you stick with unsigned, the code will work:

 unsigned int start = gettickcount(); DoLongRunningOperation(); unsigned int elapsedTime = static_cast<unsigned int>(gettickcount()) - start; 

The reason you want to stick with unsigned is because using the modulo arithmetic requires the asymmetric arithmetic, which you need in this case.

0
source

Source: https://habr.com/ru/post/949933/


All Articles