clock() function


 
Thread Tools Search this Thread
Top Forums Programming clock() function
Prev   Next
# 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
 
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
drv_hztousec(9F)					   Kernel Functions for Drivers 					  drv_hztousec(9F)

NAME
drv_hztousec - convert clock ticks to microseconds SYNOPSIS
#include <sys/types.h> #include <sys/ddi.h> clock_t drv_hztousec(clock_t hertz); INTERFACE LEVEL
Architecture independent level 1 (DDI/DKI). PARAMETERS
hertz The number of clock ticks to convert. DESCRIPTION
The drv_hztousec() function converts into microseconds the time expressed by hertz, which is in system clock ticks. The kernel variable lbolt, whose value should be retrieved by calling ddi_get_lbolt(9F), is the length of time the system has been up since boot and is expressed in clock ticks. Drivers often use the value of lbolt before and after an I/O request to measure the amount of time it took the device to process the request. The drv_hztousec() function can be used by the driver to convert the reading from clock ticks to a known unit of time. RETURN VALUES
The number of microseconds equivalent to the hertz parameter. No error value is returned. If the microsecond equivalent to hertz is too large to be represented as a clock_t, then the maximum clock_t value will be returned. CONTEXT
The drv_hztousec() function can be called from user, interrupt, or kernel context. SEE ALSO
ddi_get_lbolt(9F), drv_usectohz(9F), drv_usecwait(9F) Writing Device Drivers SunOS 5.11 16 Jan 2006 drv_hztousec(9F)