Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,376 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,156 people online right now. Registration is fast and FREE... Join Now!




problem with clock() function!

 
Reply to this topicStart new topic

problem with clock() function!

Siavash
post 29 Sep, 2006 - 09:08 AM
Post #1


New D.I.C Head

*
Joined: 29 Sep, 2006
Posts: 3


My Contributions


Hi friends,
I've made a lookup table for sqrt of some numbers to have a fast sqrt function. I'm using clock() function to calculate cpu usage of my function. I call SQRT function too many times and check cpu usage, but in some cases when I call it more times, its cpu usage comes down.
Here this function is called for 3,000,000,000 times and cpu clock usage is about 2,310,000. But when I call it for 4,000,000,000 times the cpu clock usages comes down to 470,000!
Please guide me about it. Here's the related code:
CODE
unsigned long int     count, max = 4000000000;// increasing 'max' from 3,000,000,000 to 4,000,000,000 decreases cpu usage!
    double     input = 0.0;
    double     step = MAX_INPUT / (double)max;
    clock_t     qTime, uTime;
    
    // examine quick sqrt function
    qTime = clock();
    
    for(count = 0, input = 0.0; count < max; count++, input += step)
    SQRT( input );
    
    qTime = clock() - qTime;
    //..
    
    cout<<"cpuClk = "<< qTime<< endl;
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 29 Sep, 2006 - 09:37 AM
Post #2


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


clock_t is defined like this in time.h
CODE

typedef long clock_t;


So when you iterate through the loop a lot of times, the time difference will obviously increase.
At one point, the value of the time difference will exceed the range of the long data type.
And that is why the end result is garbage and it makes you feel that the CPU Clock time has reduced.
User is offlineProfile CardPM

Go to the top of the page

Siavash
post 30 Sep, 2006 - 05:12 AM
Post #3


New D.I.C Head

*
Joined: 29 Sep, 2006
Posts: 3


My Contributions


QUOTE(born2c0de @ 29 Sep, 2006 - 10:37 AM) *

clock_t is defined like this in time.h
CODE

typedef long clock_t;


So when you iterate through the loop a lot of times, the time difference will obviously increase.
At one point, the value of the time difference will exceed the range of the long data type.
And that is why the end result is garbage and it makes you feel that the CPU Clock time has reduced.


Dear Friend,
As you wrote "At one point, the value of the time difference will exceed the range of the long data type.
", so I checked it and found that "point" equal to 3,865,470,566 call times. Where it's increased to 3,865,470,567(just one more call time), clock numbers from 3,120,000 comes down to 330,000! These are not just numbers. I can obviously feel it! I see that speed is realy increased!
Anyway calling the SQRT function just once more than before must not lead to over flow of long data type. Because its range is too much more than 3,120,000( it's about 4,000,000,000) and one more call time is not so time consuming.
And there's another problem either! The numbers are not same always! Sometimes it takes 3,120,000, sometimes 3,200,000 and sometimes other numbers! While the program is not changed and I'm running it again and again!
Do have any idea about it? sad.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:06AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month