![]() |
|
#1
|
|||
|
|||
CPU Time in time.hHey
the cpu timing function below...does anyone know how this function works? I have been using it but am unaware in what capacity it is actually timing. Is it a number of clock ticks or is it elapsed time overall from start to finish? Does it specifically refer to this usage of the CPU or will it include all other uses of the CPU during the timing period. Cheers all CPP / C++ / C Code:
Last edited by LuciWiz : 04-May-2006 at 15:57.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|||
|
#2
|
|||
|
|||
Re: CPU Time in time.hit is absolute time according to the system clock. the clock "ticks" CLOCKS_PER_SEC times per second (obviously) and the clock() function returns the number of clock ticks, therefore the elapsed time, since your process started.
|
|
#3
|
|||
|
|||
Re: CPU Time in time.hQuote:
The actual values returned by clock() are "something" relative to process startup (not necessarily zero the start of your code). The difference between values returned by two calls to clock() is supposed to represent (approximately) the elapsed processor time (whatever that means) between the events. Note that the return variable type of function clock(), time_t, is an integer data type, so, depending on how CLOCKS_PER_SEC is defined, that expression in your code might suffer from truncation caused by integer division. My personal research (Borland, Microsoft, GNU compilers) has found the following: The <time.h> header for Borland bcc32 defines CLOCKS_PER_SEC as 1000.0 For Microsoft Visual C++ I find CLOCKS_PER_SEC defined as 1000 For Cygwin/GNU gcc, CLOCKS_PER_SEC is defined as (time_t)1000 Note that the actual value of CLOCKS_PER_SEC doesn't have to be 1000, but it is for the three implementations that I mentioned above. GNU gcc version 4.1 on my Linux system defines CLOCKS_PER_SEC as 1000000l (a long int, which is still 32-bits on that particular system). So, Borland is the only one that gives the answer that you might hope for, and that's actually wrong, since CLOCKS_PER_SEC is supposed to be defined as the same data type the return value of the function clock(). Two methods come to mind to make sure that it does the right thing. 1. Force the expression to use floating point arithmetic. Something like CPP / C++ / C Code:
2. Use the standard library function difftime(). This computes the difference between two time_t variables and converts to a double: CPP / C++ / C Code:
With a simple loop to perform 10000000 double precision additions, I got these results: GNU Code:
Microsoft Code:
Borland Code:
You may not be worried about portability if you find something that works for you, but I just thought I would mention it just in case it ever comes up. I vote for difftime, since it is a standard function defined for the data types that we need for our results. Regards, Dave Last edited by davekw7x : 04-May-2006 at 17:58.
|
Recent GIDBlog
Configuring iptables for Webmin Servers Index Module by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Time Card | amir_b | C Programming Language | 1 | 21-Apr-2006 14:58 |
| Simulation Problem | wu_weidong | C++ Forum | 7 | 12-Mar-2005 22:56 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| time Problem | zuzupus | MySQL / PHP Forum | 9 | 24-Jul-2003 07:02 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The