clock() function


 
Thread Tools Search this Thread
Top Forums Programming clock() function
# 1  
Old 06-10-2005
clock() function

Hey all,

i need a program to get the CPU ticks at certain points of my program. So, i thought about using the clock function, but i'm having a hard time figuring out how it really works. I wrote this simple program to try to understand it but it made me feel more confused:

#include <stdio.h>
#include <string.h>
#include <time.h>

clock_t start, end;
double cpu_time_used;

int main(int argc, char* argv[])
{
clock_t start, end;
double cpu_time_used;
long int i;

for (i=1; i<ITERATIONS; i++)
;
start = clock();

for (i=1; i<1000000000; i++)
;
end = clock();

printf("start: %d\n",start);
printf("end: %d\n",end);

cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("cpu_time_used: %f\n",cpu_time_used);

return 0;
}

when i run my program with ITERATIONS = 100000, i get as result
start: 0
end: 2760000
cpu_time_used: 2.760000

it also happens when ITERATIONS = 1.

Why is that?!?

thanks in advance,
Breno Kastrup
# 2  
Old 06-10-2005
A few clues like what computer, what os, and what compiler would help Smilie But in general...
for(i=1; i<1000000; i++);
is really equivalent to
i=1000000;

and your compiler may be smart enough to remove the loop especially if the optimizer is turned on. Look at the options for your compiler to be sure that you aren't optimizing. If you are not optimizing, specify the option to use a symbolic debugger. Some compilers that would otherwise remove a loop will leave it in under that circumstance. If you know your systems assembler, use the the option to produce assembly code to see what is happening. You also may have a #pragma available to influence stuff like this...check you compiler's docs for that.
# 3  
Old 06-10-2005
Yes, your compiler is most likely optimizing the code. If you work with gcc, this is the second level of optimization, represented by

gcc -O2.

option (and this is default). You might want to compile your program with

gcc -O0

which removed all optimizations, or might want to #include math.h and include some math operation in the loop. For example,

for (i=1; i<1000000000; i++)
sqrt(i);
;

This gives the compiler something to do, and it would not be able to optimize it.

Hope that helps.

Kapil Sharma
# 4  
Old 06-10-2005
A very important point - the C standard does not say anyhting about the granularity of clock() - a compiler can have it check time once a second and increment the
variable by CLOCKS_PER_SEC.

This means it is possible depending on your implmentation, that you can get zero, CLOCKS_PER_SEC, CLOCKS_PER_SEC * 2 and so on, never getting any intermediate value. Don't use clock() if you need high granularity.

use gettimeofday() --
# 5  
Old 06-10-2005
I think that clock() is cpu time while gettimeofday() is wallclock time. Maybe getrusage() should be used... But I have never used clock().
# 6  
Old 06-13-2005
gettimeofday() returns a timeval struct. You can get a start value and an end value.

I think I posted on the forums how to do a delta of the times in timeval structs.
getrusage() returns CPU time, which is more useful than elapsed time on a multiprocessing box.

In the event you want cpu information, just using
Code:
time <filename>

at the commandline gives you the same information without having to instrument your code for getrusage().
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

cron: Clock generating

Hello. I would like to do this : A_VAR_FLAG = "ABCD" +------------+...................................+---------+ ...............................|.................|...................................|.............|... (7 Replies)
Discussion started by: jcdole
7 Replies

2. Windows & DOS: Issues & Discussions

Clock doesn't tick

This is a strange one, I've never seen anything like it; the realtime clock doesn't tick while the computer's idle, only when you're watching it. Leave for 3 hours and it'll be 3 hours off. It still advances when it's off however, or the time would be far more incorrect than it is. About all... (10 Replies)
Discussion started by: Corona688
10 Replies

3. UNIX for Dummies Questions & Answers

Showing Clock

Is it possible to display the clock (timing) on the screen all the time. (3 Replies)
Discussion started by: vino.paal
3 Replies

4. Programming

problem with clock()

#include<iostream> #include<time.h> using namespace std; int main() { system("date"); clock_t start = clock(); int i=9*8; while(i--) { int j=9999999; while(j--); } clock_t end = clock(); double elapsed =... (4 Replies)
Discussion started by: johnbach
4 Replies

5. Programming

clock cycle count

Hello everybody! Is there a way to count the clock cycles (that a program took to finish) in C? thanx:o (5 Replies)
Discussion started by: nicos
5 Replies

6. UNIX for Dummies Questions & Answers

Clock in Unix using awk

Hey everyone! Can someone help me, i need to make a program using awk, that displays the current time (hh/mm/ss), i would really apreciate it! Thanks! Alex. (4 Replies)
Discussion started by: alex_omul
4 Replies

7. Solaris

Bugs with clock()

Hi there!!! Need your help in solving some tricky problems. Since clock() as such is buggy on SUN OS 5 we have started using gettimeofday() in our RTOS applications based on Solaris 9. The problems we actually encountered previously were - the applications kind of freeze/hang eternally on... (1 Reply)
Discussion started by: smanu
1 Replies

8. UNIX for Advanced & Expert Users

clock change

Hi We had a AIX box built last year but was set to the correct GMT time, but using DST time zone. In march this year the clocks went forward without issues. (if I remember a couple of weeks early due to the DST zone) This year we decided to change the clock to the correct time zone before... (0 Replies)
Discussion started by: markab2
0 Replies

9. UNIX for Dummies Questions & Answers

Clock Trouble

Hey ppl, i was wonddering, in mandrake, how to get the clok to display the time in non-military format....hehe thank you im just tired of looking at 18:00 hehe thank you (2 Replies)
Discussion started by: LolapaloL
2 Replies
Login or Register to Ask a Question