View Full Version : Timer in Linux
dav_mt
04-18-2008, 06:16 PM
Hi I need to find the time between two events in my code. Mainly I'm receiving packets over Bluetooth and as soon as I receive a certain amount of packets I launch a video player using fork() and exec().
On every packet I receive I increment a packet counter and on the nth packet I execute the video player.
Basically what I need is a timer from: the time the first packet arrives till the time the nth packet arrives
I used the standard time_t function but I need something more accurate. Ideally something up to millisecond accuracy would be perfect.
thanks
foxman
04-18-2008, 06:45 PM
Well, the C standard library offers the clock (http://www.cplusplus.com/reference/clibrary/ctime/clock.html) function, who's more accurate by a couple orders of magnitude than the time function usually (it's implementation specific if i remember well).
Otherwise, i couldn't help you much. You could try searching in the section 2 of man pages.
EDIT: Just by the way, on Visual Studio 2005, the clock function is accurate to the millisecond.
cyberfish
04-18-2008, 08:42 PM
look up gettimeofday(). The data structure allows for microsecond accuracy, but the actual accuracy is implementation defined (on the Linux IIRC it's somewhere around 1x10^-5s)
clock() is not a good idea IMHO, because it does different things on different platforms. On some, it measures the wall time, and it measures CPU time in others, which can be quite significantly different from the wall time, especially if you do a lot of IO (networking for example).
medievalelks
04-18-2008, 10:52 PM
There's a Linux forum on this site...just sayin'.
laserlight
04-18-2008, 11:52 PM
There's a Linux forum on this site...just sayin'.
Indeed. Moved to Linux Programming.
dav_mt
04-19-2008, 06:09 AM
thanks for the help ppl
working gr8 with gettimeofday() function and sorry for posting in the wrong forum
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.