GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-May-2006, 15:54
druid druid is offline
New Member
 
Join Date: Feb 2006
Posts: 22
druid is on a distinguished road

CPU Time in time.h


Hey


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:
clock_t start, end;
double cpuTime;

start = clock();

//....bit to be timed

end = clock();


cpuTime= (end-start)/ (CLOCKS_PER_SEC);

Last edited by LuciWiz : 04-May-2006 at 15:57. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 04-May-2006, 15:58
ubergeek ubergeek is offline
Awaiting Email Confirmation
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: CPU Time in time.h


it 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  
Old 04-May-2006, 16:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: CPU Time in time.h


Quote:
Originally Posted by druid
Hey
does anyone know how this function works?

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:
    cpuTime= (end-start)/ (double)CLOCKS_PER_SEC;

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:
    cpuTime = difftime(end, start)/CLOCKS_PER_SEC;

With a simple loop to perform 10000000 double precision additions, I got these results:

GNU
Code:
With integer arithmetic, cpuTime = 0.000000 With floating arithmetic, cpuTime = 0.063000 With difftime, cpuTime = 0.063000

Microsoft
Code:
With integer arithmetic, cpuTime = 0.000000 With floating arithmetic, cpuTime = 0.062000 With difftime, cpuTime = 0.062000

Borland
Code:
With integer arithmetic, cpuTime = 0.125000 With floating arithmetic, cpuTime = 0.125000 With difftime, cpuTime = 0.125000




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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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 · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:35.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.