tags:

views:

403

answers:

4

How we can implement our own timer function in Windows without using Library functions? Should we deal with assembly language instructions?

+4  A: 

This depends on the precision and resolution you require.

Assuming you need more than second precision, it's popular to wrap/use the RDTSC instruction. But you can also just use the Win32 API, the QueryPerformanceCounter() is widely used for this.

unwind
A: 

Many APIs like Intel's Threading Building Blocks (http://threadingbuildingblocks.org), OpenMP (http://openmp.org), SDL (http://libsdl.org) or SFML (http://sfml-dev.org), and I think also OpenGL (http://opengl.org) implement a timer or a wrapper around a timer.

Saying "without using Library functions" is a bit vague: Which library do you mean specifically? In the end, you have to use some library function, or inline assembly, because C++ doesn't have timing functionality in the core language. And without any timer processor or some other timing hardware in whichever form (*), there is no way to tell the time, not vaguely, not exactly.

(*) That includes internet time providers.

phresnel
not using any standard C library functions or windows APIs
Sarath
A: 

Disclamer: please don't ever use code like this

typedef long time_t;

time_t time(time_t *);
int  printf(const char * __restrict, ...);

int main()
{
  printf("%d\n", time(0));
}

is a complete program that compiles with just 'gcc foo.c'
It uses the standard library, but I'm not sure it's possible to do anything without it. For further entertainment, it does it without header files (warning: this will break much compatibility)
it gives only second resolution, but you get the idea.

cobbal
The only thing you've achieved by not using header files is to have compatibility problems. In essence you've manually added the contents of those headers - the meaning of the code is the same.
Richard Corden
yes, I was trying to point out how built in the standard library is
cobbal
I think that code is irrelevant, as the OP was asking for *timing* without libraries, not *printing the time* without libraries. And the longer I look at the code, the more confused I get what exactly you wanted to say with it. Libraries are a distinct concept then header files. Better I get off :/
phresnel
A: 

get the atomclock time from an internet time service.

else you will always deal with some sort of library-functions with which you can talk to the BIOS, which is the part that makes the tick-tock from the Quartz visible to the OS.

Peter Miehle