Click to See Complete Forum and Search --> : Precision time measurements in GNU GCC
dude_1967
October 10th, 2002, 09:01 AM
Dear Gurus,
I would like to do some performance timing in a cross-development project.
What is the GNU equivalent of of the Microsoft function INT32 ::GetTickCount(void)? Please also indicate which header is needed.
Are there GNU equivalents to
BOOL ::QueryPerformanceCounter(LARGE_INTEGER*)
and
BOOL ::QueryPerformanceFrequency(LARGE_INTEGER*)?
Thanks a bundle.
Chris.
:)
stober
October 10th, 2002, 11:49 AM
Those are MS Windows os functions and have nothing to do with the compiler.
JMS
October 10th, 2002, 03:36 PM
Try the Ansi function clock()....
This function gives you the number of clock cycles since the process began. To get the number of seconds you'd need to do the following calculation...
CLOCKS_PER_SEC
duration = (double)(clock()) / CLOCKS_PER_SEC;
where CLOCKS_PER_SEC is the number of clock ticks per seconds... I believe that CLOCK_PER_SEC is an ANSI defined macro..
dude_1967
October 11th, 2002, 04:37 AM
Thanks,
I can use clock() for this particular application. The needed precision is low. I had forgotten that this standard ANSI function exists (CLOCKS_PER_SEC is ANSI, header file is <time.h>).
Indeed ::GetTickCount() and the performance counter functions are Microsoft WIN32 API functions. I was hoping that the C or C++ compiler of GCC supports similar (non-ANSI) functions, especially high-frequency counters for higher-precision work. Remember, various incarnations of GCC supports such functions as ::sleep() and ::nanosleep() which are non-ANSI.
Maybe someone has experience with high-precision timers in GCC and can provide additional information. I do not want to do anything like read the Pentium time stamp counter since I would prefer to remain CPU-independent for this particular application.
Again, thanks.
Sincerely,
Chris.
:)
AnthonyMai
October 11th, 2002, 08:35 AM
Maybe someone has experience with high-precision timers in GCC and can provide additional information. I do not want to do anything like read the Pentium time stamp counter since I would prefer to remain CPU-independent for this particular application.
The words "platform independent" and "high precision timer" do not come close to each other, dude. Any physical time measurement has to be implemented one way or another by hardware, either by some sort of BIOS system clock maintained by battery power even if the system is powered down, or by some sort of CPU clock cycle counter or high performance counter, which may reside on the CPU or on the motherboard, or on other circuitry. And hardware implementations are certainly very platform-dependent.
Among other things, if the user simply pry out that small piece of crystal and replace with another, in an attempt of "over-clock" your system. Then all your effort of trying to get a correct reading of the time at all, will fail. No I am not talking about the fortune telling crystal, I am talking about the crystal oscillator.
Time measurement is very platform dependent: It does not just depend on the software platform (the OS), more over, it depends on the specific brand of hardware and yes, CPU. Do NOT always assume Windows OS will give you QueryPerformanceCounter(). You have to look at the motherboard hardware to see if it is there. Do not assume you will have RDTSC assembly instruction. You have to look at what CPU you have. On some hardware, some platform, time measurement is simply impossible altogether.
OK, there is an ANSI function clock(). Still clock() boils down to platform dependent stuff underneath that may or may not exist, and may or may not provide the precision you need.
And even a simple YES or NO answer to the very question "Is high precision time measurement doable or not", is platform and CPU dependent. So how could you get away from having to look at platform dependent issues, in an invain effort of finding a platform/CPU independent solution?
You can't.
JMS
October 11th, 2002, 10:11 AM
Clock() provides milisecond granularity or better so it's not exactly low granularity... I mean if all you need is second granularity you could get that using the time() function..
Again both clock() and time() are ANSI standard C/C++ and are thus widely availible on any ANSI copiler across different platforms...
Good Luck..
PaulWendt
October 11th, 2002, 10:20 AM
Originally posted by AnthonyMai
The words "platform independent" and "high precision timer" do not come close to each other, dude.
He was probably looking for something has the same interface
across all platforms even though it is implemented differently.
--Paul
dude_1967
October 11th, 2002, 10:44 AM
Gurus,
In this particular example, I will be timing computations in the range of several seconds to several minutes. The resolution of clock() is sufficient.
High-resolution timing is a rich topic. It is relevant in many aspects of my work and research investigations. Timing is especially crucial in our microcontroller applications. I am always pleased when a compiler / library / computer platform or some other on-board peripheral offer some access to a calculable, high resolution time source.
My favorite is, in fact, the Pentium time stamp counter, which can be accessed for x86 processors above 586 in unprotected mode.
Perhaps there will be some improvements in PC and microcontroller industry specification standards which might one day provide the hardware basis for software access to sub-nanosecond counters across all systems.
I guess this one could take a while...
Chris.
:cool:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.