Bad naming, time is already defined in ctime
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
clock_t start = clock();
cout << "press enter...";
cin.ignore();
clock_t finish = clock();
double doneTime = float(finish-start)/CLOCKS_PER_SEC;
cout << "you took " << doneTime << " seconds\n";
return 0;
}
firstPerson
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15
http://linux.die.net/man/3/clock
clock() determines (to some approximation) the amount of CPU time used by a program.
Waiting around for I/O typically does not count.
If you want to measure the amount of elapsed time, as by your watch for example, then use the time() functions.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
Consider using gettimeofday() which keeps track of seconds and microseconds since the epoch. To compare them, subtract the two fields individually, then multiply one (or divide the other) and add the differences; then convert as needed.
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
http://linux.die.net/man/3/clock
clock() determines (to some approximation) the amount of CPU time used by a program.
Waiting around for I/O typically does not count.
If you want to measure the amount of elapsed time, as by your watch for example, then use the time() functions.
The clock() function returns an approximation of processor time used by the program
Waiting for I/O counts as it constitute to time used by processor for waiting
firstPerson
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15
Is gettimeofday() portable to windows as well? I am using Linux right now, but I will want this game to be portable. It looks like SDL can measure time in milliseconds, so I may end up using that. If gettimeofday() is portable it looks like it may work better.
If you want to use windows, check out queryPerformanceCounter, that's what boost uses if available.
EDIT: Oh I see, why not just use boost?
firstPerson
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15
Waiting for IO uses no processor time. The processor is busy running other processes.
Maybe I'm confused. The clock() function returns an approximation of processor time used by the program. When I/O operation occurs, CPU usually sends the job to a IO processor and proceeds with other jobs until it gets interrupted right? So in that wait period, the CPU was busy doing something else, but there is still that wait period. So why shouldn't for example,
long s = clock();
cin.get();
long e = clock() - s;
'e' be some time unit? I'm not saying that CPU just waits for IO to be done, but I'm saying that there is this IO waiting time, in which although CPU does other useful work.
firstPerson
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15
Because the clock() function returns the time that the processor uses [Ifor this task[/I] and not for the amount of elapsed time. The perceived need, when it was written, was to understand how much CPU-time each particular task was using. Elapsed time has only a vague connection to that (elapsed time can never be less than CPU time).
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
> but I'm saying that there is this IO waiting time, in which although CPU does other useful work.
Yes, other useful work, incrementing the clock() for the other processes which are having useful work done.
If you want sub-second precision, then the OP needs to state which OS/Compiler is being used.
gettimeofday() has microsecond precision, but the accuracy can vary from one machine to another. For example, the usec field could be stuck at zero, and the sec field increments once a second, and it would be within spec (although not so useful). Do NOT expect that the usec field will always tick once per microsecond.
Even queryperformancecounter, or the "raw" RDTSC instruction is not without issues.
http://en.wikipedia.org/wiki/Time_Stamp_Counter
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
Question Answered as of 1 Year Ago by
firstPerson,
Salem,
griswolf
and 1 other