Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
clock() function busted
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
C_Hird
Tux's lil' helper
Tux's lil' helper


Joined: 17 Feb 2003
Posts: 78
Location: Ontario Canada

PostPosted: Wed Mar 26, 2003 1:03 am    Post subject: clock() function busted Reply with quote

Hi

Just in case anyone wants to use the clock function it is not working!
I am using GCC 3.2.2.

This codes does not work
Code:

#include<iostream.h>
#include<time.h>

class timer{
   clock_t start;
public:
   timer(); // constructor
   ~timer(); //destructor
};

timer::timer()
{
start = clock();
}

timer::~timer()
{
clock_t end;
cout << "timer : " << clock() << '\n';
end = clock();
cout << "Clock end : " << end << '\n';
cout << "Clock start : " << start << '\n';
cout << "Elapsed time : " << ((end-start) / CLOCKS_PER_SEC) << '\n';
}

main()
{
timer ob; // create an instance of the timer class - calls constructor
char c;

// create a delay loop between the object creation and the return from the program

cout << "Press a key followed by ENTER : ";
cin >> c;

// return will create a call to the destructor

return 0;
}




This code does!

Code:

#include<iostream.h>
#include<time.h>
#include<sys/times.h>

class timer{
   clock_t start;
public:
   timer(); // constructor
   ~timer(); //destructor
};

timer::timer()
{
tms buffer;
start = times(&buffer);
}

timer::~timer()
{
tms buffer;
clock_t end;
end = times(&buffer);
cout << "Clock end : " << end << '\n';
cout << "Clock start : " << start << '\n';
cout << "Elapsed time : " << ((float)(end-start)/CLOCKS_PER_SEC) << '\n';
}

main()
{
timer ob; // create an instance of the timer class - calls constructor
char c;

// create a delay loop between the object creation and the return from the program

cout << "Press a key followed by ENTER : ";
cin >> c;

// return will create a call to the destructor

return 0;
}


Only change is using the times function which returns a clock_t variable which is the same values as those returned by the clock() function. The additional cast to a float is to take into account the fact that it is taking less than 1 second of CPU time to process the tests that I have run. Either that or the description provided is incorrect. The documentation supplied at GNU docs says the return value is the number of clock tics which has to be divided by 1000000 to get to a second! This is obviously flawed as well?
I would appreciate someone checking my findings before I go add a bug.
Also checkout the problems that I found with strlen() function to see if this is also a problem or a coding issue. G++ STRLEN() problem in this forum...

No one else has added any other comments so I can only assume this is already known about?

Chris.. :wink:
_________________
trying Gentoo, running RedHat 8.0, removed SuSe 7.2 argh!
Back to top
View user's profile Send private message
C_Hird
Tux's lil' helper
Tux's lil' helper


Joined: 17 Feb 2003
Posts: 78
Location: Ontario Canada

PostPosted: Wed Mar 26, 2003 4:01 pm    Post subject: Is this the wrong forum Reply with quote

Hi

Not sure why but no takers on the request?? The forum says post request for c problems etc but looking through doesnt look like many respond to programming issues.

As a side not I think something is very wrong with either the compiled objects are Gentoo kernel, The clock on my system seesm to loose time and gain time at varying opportunities. I have written another program which should allow elapsed time to be calculated (stop watch) but it suffers from the same kind of issues in that the times reported are minute because of the returned values from the times function.

If this is a problem it needs to be identified, I dont want to go down the path of reporting something which is not a problem with the system or compiler but rather a coding or conceptual problem, I would therefore like clarifiaction from others on the forum!

Chris.. :cry:
_________________
trying Gentoo, running RedHat 8.0, removed SuSe 7.2 argh!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum