#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    12
    Rep Power
    0

    Question 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 :confused:
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,646
    Rep Power
    4248
    Maybe your normalSearch() function is working too fast, which is why timeStart = timeEnd. See if gettimeofday() will help?
  4. #3
  5. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    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
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,646
    Rep Power
    4248
    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.
  8. #5
  9. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    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.
    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

IMN logo majestic logo threadwatch logo seochat tools logo