Advertisement

High-res timers in Unix?

Started by August 31, 2002 06:20 AM
3 comments, last by Ziphnor 22 years, 5 months ago
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.
Perhaps i should point out that if the timers differ on different unix types, im interested in Linux.
Advertisement
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.
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.


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

This topic is closed to new replies.

Advertisement