Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timespec structure

Status
Not open for further replies.

maur3r

Technical User
Aug 28, 2006
34
0
0
PL
Hi I heart that for measuring a time of loop execution in nanoseconds struct timespec could be used. However, I didn't find any info how to implement it into C code. I am wondering if it works more or less the same as clock() function
Code:
int s,f;
float time;
s=clock();
/*loop*/
f=clock();
time=(f-s)/CLOCKS_PER_SEC
or maybe totally different. Also what libraries should I include. I am pretty sure that <unistd.h> must be used.
I would appreciate any kind of help. Thanks in advance.

Martin
 
You have to be careful about what you're looking at. You have

(1) CPU time used including time spent in system routines
(2) CPU time used excluding time spent in system routines
(3) Elapsed time

I think clock gives you (1). 1s in CPU time is something very strange. It doesn't always equate to 1s elapsed time. Sometimes it is faster, sometimes it is slower.

Also I don't know how accurately you can get the time in nanoseconds. The CPU can run that fast but whether you can get any information is something else. With the speeds of systems nowadays, it has to be a very big loop before you'll even see anything.
 
Which OS and compiler are you using?

For Linux / Unix types, then look at the gettimeofday() function, which returns a struct containing seconds and microseconds. Although it stores microseconds (the accuracy), the actual precision for your machine can vary (it could add say 10000 every 10mS and be compliant).

For windows, then look at the queryperformancecounter function.

Or if you're using the gcc compiler on a pentium style processor, then you could use the rdtsc instruction to read the fast CPU clock as illustrated in one of my replies in the linked thread.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top