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
9 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
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
2. UNIX for Advanced & Expert Users
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
3. Solaris
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
4. UNIX for Dummies Questions & Answers
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
5. Programming
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. Programming
#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
7. UNIX for Dummies Questions & Answers
Is it possible to display the clock (timing) on the screen all the time. (3 Replies)
Discussion started by: vino.paal
3 Replies
8. Windows & DOS: Issues & Discussions
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
9. Shell Programming and Scripting
Hello.
I would like to do this :
A_VAR_FLAG = "ABCD" +------------+...................................+---------+
...............................|.................|...................................|.............|... (7 Replies)
Discussion started by: jcdole
7 Replies
clock(3C) clock(3C)
NAME
clock() - report CPU time used
SYNOPSIS
DESCRIPTION
returns the amount of CPU time (in microseconds) used since the first call to The time reported is the sum of the user and system times of
the calling process and its terminated child processes for which it has executed , or (see wait(2) , system(3S), and popen(3S)). To deter-
mine the time in seconds, the value returned by should be divided by the value of the macro
The resolution of the clock varies, depending on the hardware and on software configuration.
If the processor time used is not available or its value cannot be represented, the function returns the value
WARNINGS
The value returned by is defined in microseconds for compatibility with systems that have CPU clocks with much higher resolution. Because
of this, the value returned wraps around after accumulating only 4295 seconds of CPU time (about 72 minutes).
DEPENDENCIES
The default clock resolution is 10 milliseconds.
SEE ALSO
times(2), wait(2), system(3S), thread_safety(5).
STANDARDS CONFORMANCE
clock(3C)