Evaluate time in C++

• 04-02-2008
nghoanglinh223
Evaluate time in C++
anyone can help me??
How to evaluate time between 2 event!!
• 04-02-2008
Elysia
var1 = clock();
// Do something
var2 = clock();
diff = var2 - var1;

Perhaps?
• 04-02-2008
nghoanglinh223
thanks !! I'll try!!!
^.^
P/s can you tell me the types of var1 and var 2?? (int/ double/ or others??)
• 04-02-2008
Elysia
The return type of clock (according to MSDN, clock_t).
• 04-02-2008
nghoanglinh223
Can you explain clearly!! T.T I don't understand!! :((
And time in here count by second or other??
• 04-02-2008
Elysia
What do you not understand?
• 04-02-2008
nghoanglinh223
Can you explain if I use types clock_t for var1 and var2 .... they'll counted by second or other,wont' they??
And what's the difference between clock_t and MSDN??
Thanks you!
• 04-02-2008
matsp
Quote:

Originally Posted by nghoanglinh223
Can you explain clearly!! T.T I don't understand!! :((
And time in here count by second or other??

The time is not seconds, but "ticks", and there is #define constant in clock.h called CLOCKS_PER_SEC, which you can divide the result by to give you seconds. If you are measuring relatively short times, you should probably also convert your time to float or double.

--
Mats
• 04-02-2008
Elysia
Quote:

Originally Posted by nghoanglinh223
Can you explain if I use types clock_t for var1 and var2 .... they'll counted by second or other,wont' they??

The function returns some sort of "ticks," that is defined by a special define somewhere.
clock_t is a type which clock() returns, simply put. You should use that type to ensure the code works.

Quote:

And what's the difference between clock_t and MSDN??
clock_t is a type and MSDN is a place on the web for documentation about programming.
• 04-02-2008
matsp
Quote:

Originally Posted by nghoanglinh223
And what's the difference between clock_t and MSDN??
Thanks you!

MSDN is "MicoSoft Developers Network", and "clock_t" is the type that clock() returns as per the ANSI C89/ISO C90 standard (and earlier pseudo-standards).

So to compare those is like comparing an apple to a car - they don't compare very well, because they have completely different purpose and meaning.

--
Mats
• 04-02-2008
nghoanglinh223
Quote:

Originally Posted by matsp
The time is not seconds, but "ticks", and there is #define constant in clock.h called CLOCKS_PER_SEC, which you can divide the result by to give you seconds. If you are measuring relatively short times, you should probably also convert your time to float or double.

--
Mats

Can you give me a example,plz!! ^.^
I have tried in this code:

int var1,var2,diff;

var1 = CLOCKS_PER_SEC;

ab.saveToFile("ATM.txt",key);

var2= CLOCKS_PER_SEC;

cout << " :" << var2;

diff = var2-var1;

cout << endl<<diff <<endl;

and the result is var1 = 1000;
var 2 =1000
diff = 0 :|

And i can't include clock.h

Thanks!!
• 04-02-2008
Elysia
Hello? You are supposed to call clock(). And we already told you the type is clock_t.

Code:

```clock_t var1,var2,diff; var1 = clock(); ab.saveToFile("ATM.txt",key); var2 = clock(); cout << " :" << var2; diff = var2-var1; cout << endl<<diff / CLOCKS_PER_SEC <<endl;```
It's supposedly residing in time.h.
The documentation also explicitly states
Quote:

(elapsed time in seconds times CLOCKS_PER_SEC)
• 04-02-2008
matsp
Yes, you should use var1= clock() and var2 = clock(), then DIVIDE the resulting difference by CLOCKS_PER_SEC().

--
Mats
• 04-02-2008
nghoanglinh223
Thanks you so much!!
:)
• 04-02-2008
cyberfish
I would skip clock() and go for gettimeofday() if I were you. clock() is kind of not portable. It is defined everywhere (as far as I can tell), but have different meanings. IIRC on GCC/Windows, it measures the wall time (real time), on GCC/Linux, it measures CPU time (sometimes significantly less than real time).

Or, if you only need second precision, there is always time().