|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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 ![]() |
|
#2
|
||||
|
||||
|
Maybe your normalSearch() function is working too fast, which is why timeStart = timeEnd. See if gettimeofday() will help?
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > How to calculate duration of a process in seconds |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|