Click to See Complete Forum and Search --> : Time delay C++


IL96
11-08-2000, 09:00 AM
Hi guys, how do i create a time delay in nanoseconds / miliseconds in C++ ?

Humus
11-08-2000, 10:14 AM
Something like this (in VC++):


//Enable the RDTSC instruction
#ifndef RDTSC
#define RDTSC __asm _emit 0x0F __asm _emit 0x31
#endif

#pragma warning(disable: 4035)
__inline __int64 GetCycleNumber(){
__asm {
RDTSC
}
}

__int64 GetHz(){
LARGE_INTEGER t1,t2,tf;
__int64 c1,c2;

QueryPerformanceFrequency(&tf);
QueryPerformanceCounter(&t1);
c1 = GetCycleNumber();
_asm {
MOV EBX, 5000000
WaitAlittle:
DEC EBX
JNZ WaitAlittle
}
QueryPerformanceCounter(&t2);
c2 = GetCycleNumber();

return ((c2 - c1) * tf.QuadPart / (t2.QuadPart - t1.QuadPart));
}

void delay(int millis){
__int64 Hz = GetHz();
__int64 start = GetCycleNumber();
while (millis * Hz / 1000 < GetCycleNumber() - start);
}

Raptor^
11-08-2000, 10:42 AM
Anything particularly wrong with
void Sleep(DWord dwMilliseconds); in Windows.h for a Windows box
or
int nanosleep(const struct timespec *req, struct timespec *rem) in time.h for a linux box (I presume other Unix variants have something similar, take a look in time.h and see)

AIX has the exact same nanosleep as linux, so I expect other Unices will too

[This message has been edited by Raptor^ (edited November 08, 2000).]

IL96
11-08-2000, 11:13 AM
Hm is there a simpler way ? QueryPerformanceFrequency() and QueryPerformanceCounter() are not found and if i include winbase.h the debugger gives a lot of errors in that file.

------------------
What we're dealing with here is a total lack of respect for the law...
Prodigy

IL96
11-08-2000, 11:46 AM
Sleep() from windows.h seems to work though, thanks !

Raptor^
11-08-2000, 11:59 AM
erm, don't include winbase.h directly, its automatically included by windows.h and needs some of the other stuff that windows.h has.

Just include windows.h instead http://www.sharkyforums.com/ubb/smile.gif