Link to home
Start Free TrialLog in
Avatar of cosmowen
cosmowen

asked on

need a high-resolution timer

I need an OS- independent timer call i tried clock with a starTime = clock(); //much code finishTIme = clock(); call with a time = double(finishTime - startTime) / CLOCKS_PER_SEC;
I need something that can get down to the thousandths.  I need this to run on windows and unix

ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of efn
efn

The Adaptive Communication Environment (ACE) library has a portable high-resolution timer class.

http://doc.ece.uci.edu/Doxygen/Current/html/ace/classACE__High__Res__Timer.html

It doesn't look like it's easily separable from the rest of the library.

--efn
IBM's alphaWorks has a high resolution time stamp facility that I have used on occasion:
http://www.alphaworks.ibm.com/tech/ibmts

-bcl
Avatar of cosmowen

ASKER

ok brett i tried that one and it isn't accurate enough thanks though.

I need this to be a built in or easy addition to my code function. and it must run on unix preferably windows too
getTimeOfDay() is in milliseconds and is available on *nix boxen.
(as pointed out above -1 redundant)
yes i know and the clock time gives me more accuracy than get time of day just gave me 0.0000000 clock calls gave me 0.01000000 running in the same function  and i know it was -1 redundant to say that but just for new readers who usually don't read everything put up
Take a look at
http://www.terimber.com/text/opensrc/timer.html
Is it what are looking for?
I don't think there is an easy answer.  gettimeofday doesn't seem to be available on Windows except in Java.  The ACE class is not easy to use.  The IBM High Resolution Time Stamp Facility is only for Windows.

By the way, I think all the answers you have so far deal with elapsed time.  clock(), however, doesn't give you elapsed time.  It gives you processor time used by your process.

--efn
Is there a Sleep() api in unix? If so start a thread and use it in a loop. Create a dll that calls apropriate routine accourding to OS.

RJ
With reguards to windows...

Only problem about a timer like the one you accepted is that it basically locks up your application.

But that also depends upon how you need to use it. If all you need to do is test the time some function takes to process then that is acceptable.

But if you need to create a loop where some condition gets continuosly checked every so often then you will need to implement it in a thread. Other wise your application will lock up in that one loop and not allow you to do any other processing (Including exiting the loop because send,flag or create a condition that will break the loop once it gets started).

You might be able to use the gettimeofday() along with a thread. But the norm is to use sleep allong with a thread.

RJ