uSec delays in windows, Possible?

euan

New member
Can it be done? I've investigated using queryperformancecounter, and other weird stuff, but why can't I just have a delay() function ? :(
 
Sorry, I just read yor post title to quickly. queryperformancecounter is probably your only option then. Even then I doubt you'll get the accuracy your looking for... under windows.
 
This should solve it:

Code:
#include <windows.h>
#include <stdio.h>

#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, 2500000
		WaitAlittle:
			DEC		EBX
		JNZ	WaitAlittle
	}
	QueryPerformanceCounter(&t2);
	c2 = GetCycleNumber();
	
	return ((c2 - c1) * tf.QuadPart / (t2.QuadPart - t1.QuadPart));
}


__int64 CPU_Hz;

void delay(int micros){
	__int64 start = GetCycleNumber();

	while (GetCycleNumber() - start < micros * CPU_Hz / 10000000);
}

void main(){
	CPU_Hz = GetHz();

	printf("Hello\n");

	// delay 0.5 seconds
	delay(5000000);

	printf("World\n");

}
 
Thanks, your da man... I don't have a clue about the ASM, but it seems to make sence. This is more-or-less as far as I had got :D (don't try compile it)!

Code:
#include "stdafx.h"
#include "iostream.h"
#include <windows.h>

int main(int argc, char* argv[])
{
	printf("Hello World!\n");
	LARGE_INTEGER c1, c2, t2, freq;

	__int64 x;
	int ms = 1; //delay in milisecs
	QueryPerformanceFrequency(&freq);

	x = freq.QuadPart/1000*ms;

//delay
	QueryPerformanceCounter(&c1);
		do
		{
			QueryPerformanceCounter(&c2);
		}while(c2.QuadPart-c1.QuadPart < x);
//delay
	QueryPerformanceCounter(&t2);
	cout << c1.QuadPart << c2.QuadPart << t2.QuadPart << freq.QuadPart;
	printf("Finished!\n"); 
	return 0;
}

As youcan see it was only for milisecs, I later got round to hacking it a bit more at work, but it was just a mess!

PCs are just a pain in the ass when it comes to latencies etc, so fast, but always late... :rolleyes:

I'll give your code a go on Monday. :)
 
Back
Top