The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How to calculate duration of a process in seconds
Discuss How to calculate duration of a process in seconds in the C Programming forum on Dev Shed. How to calculate duration of a process in seconds C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 7th, 2003, 02:14 AM
|
|
Junior Member
|
|
Join Date: Sep 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
How to calculate duration of a process in seconds
Good day !
I have a problem here. I got a function call normalSearch to perform a linear search on an array. I wish to time the duration taken for the searching process. So I coded my program this way:
clock_t timeStart, timeEnd;
timeStart = clock();
searchResult = normalSearch(searchString, testArray);
timeEnd = clock();
timeUsed = (double)(timeEnd-timeStart)/CLK_TCK;
I thought it should return the timeUsed in the format of x.xx.
But weirdly enough, what I got is that, the timeStart is equal timeEnd ?
How should I modify the function so that it can calculate the time correctly ?
Thanks a lot !
SonicWave 
|

January 8th, 2003, 01:41 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Maybe your normalSearch() function is working too fast, which is why timeStart = timeEnd. See if gettimeofday() will help?
|

January 8th, 2003, 09:51 PM
|
|
Offensive Member
|
|
Join Date: Oct 2002
Location: in the perfect world
|
|
|
Timers are very inaccurate.
Under window OS timer messages are the lowest prority (except some paint msg's). They keep getting dropped back to the bottom of the msg que as more inportant msg's arrive.
CLK_TCK has been obsolete since win95, use CLOCKS_PER_SEC (CLK_TCK is #defined as CLOCKS_PER_SEC anyway).
It is in milliseconds so they both are 1000.
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
Frank Zappa
|

January 9th, 2003, 02:00 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
The WM_TIMER message may be a low priority, but getting the time from the real time clock is definitely much more accurate. This is because the clock interrupt runs on a very high priority (IIRC, only the Non-Maskable Interrupt (NMI) runs higher). The trouble with using the real time clock is that you don't know how much time is actually used by your process. All you know is that your process started at time 'x' and ended at time 'y'. During this time, the OS may have switched to running some other processes and there is no way for you to know how much time the other processes took, so you can subtract them out. If you're not running too many other things though, then using the real time clock should give a fairly accurate value.
On the other hand, the clock() function gives an approximate time used by the CPU for your program alone. It also does not include the time your program waited for I/O processes, or other external programs to complete. It also does not include the time used by other processes that had nothing to do with your program. The only trouble with this is, that the resolution is not too high. Unlike what TechNoFear says, the value of CLK_TCK and CLOCKS_PER_SEC are not the same (and not always 1000 either). In fact, this value is highly OS dependent. These are the values for different OS's that I investigated:
FreeBSD 4.6/4.7 - 128 for CLK_TCK and CLOCKS_PER_SEC
OpenBSD 3.2 - 100 for CLK_TCK and CLOCKS_PER_SEC
RedHat Linux 7.1 - 100 for CLK_TCK, 1000000 for CLOCKS_PER_SEC
The man page for clock() on RedHat Linux notes that you're supposed to divide by CLOCKS_PER_SEC to get the number of seconds used. It also notes that POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolu_tion.
|

January 9th, 2003, 10:11 PM
|
|
Offensive Member
|
|
Join Date: Oct 2002
Location: in the perfect world
|
|
Sorry, seems I was not clear enough. When I said
Code:
Under window OS timer messages are the lowest prority
I assumed you would read the rest as MS OS dependant.
Sorry if this caused confusion.
Under MS OS GetTickCount() may be better as it is the number of milliseconds elaped since booting (so will give actual tme not CPU time).
I looked also at the task to be timed
normalSearch(searchString, testArray);
and assumed it was a simple string find function and so would need around a 5 millsecond accurate timer, which is virtually impossible under MS OS without looking at the CPU's cycles.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|