Archived

This topic is now archived and is closed to further replies.

High-res timers in Unix?

This topic is 5581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im currently taking a course on algorithmics. Assignments can be handed in in either C/C++/Java. One of the first assignments is to time some sorting algorithms. I know the timers under Windoze, but i would also like to have my timing facility available under linux(where i do most of my programming, because i really like gcc/g++). I could of course use Java, but i really like working in c++. Ive been trying to figure out what timers are available in a unix enviroment, but havent really been succesful. Ive been messing with the itimers, but i cant really get them to work(anyway they are meant to be used as "alarm clocks" not timer clocks). Any ideas, the timer should at least have 100msec resolution, preferably lower.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use ''gettimeofday'' (in sys/time.h) if it''s supported (it is in Linux and BSD, but not elsewhere, if I remember correctly), otherwise use ''ftime'' (in sys/timeb.h). They''re easy to use, look in the man pages.

Share this post


Link to post
Share on other sites
Weird, i found some reference for gettimeofday somewhere and i seem to remember it saying that gettimeofday returned the time in seconds, so i assumed it had a 1 sec resolution.
I can see from the man page that this is not the case.

Thanks for the help.

Share this post


Link to post
Share on other sites


timeb stTmpTimeb;
::ftime(&stTmpTimeb);
uiBaseTime= stTmpTimeb.millitm+stTmpTimeb.time*1000;


or
timeval stTmpTimeV;
gettimeofday(&stTmpTimeV,NULL) ;
uiBaseTime=stTmpTimeV.tv_sec*1000+(tTmpTimeV.tv_usec/1000;



Advice:
Use manual in unix, if you don''t know
example: $ man ftime

Share this post


Link to post
Share on other sites