Solved

clock() not working

Posted on 2002-03-06
11
502 Views
Last Modified: 2010-05-18
#include <time.h>

....
printf("cpu= %g",clock()/CLOCKS_PER_SEC);
.....

output: cpu= 0
0
Comment
Question by:anothercto
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6844971
Make it read

printf("cpu= %f",((double) clock())/CLOCKS_PER_SEC);
0
 
LVL 86

Expert Comment

by:jkr
ID: 6844977
FYI, the way you did it, you're doing an integer division. So, if clock() < CLOCKS_PER_SEC, the result is '0'...
0
 

Author Comment

by:anothercto
ID: 6845006
actually I did use the 'double' cast. what really happens is that when I run the program, one out of 4 times, I get cpu=0.015, I get cpu= 0 the other 3 times..
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Expert Comment

by:madface
ID: 6845007
clock()/CLOCKS_PER_SEC just shows how long your your app has been running in seconds
0
 
LVL 1

Expert Comment

by:madface
ID: 6845010
clock()/CLOCKS_PER_SEC just shows how long your your app has been running in seconds
0
 
LVL 1

Expert Comment

by:madface
ID: 6845023
so 0 means the application executed really  fast
through  a  in a 6000000 empty loops in your app and see how long it  take
0
 

Author Comment

by:anothercto
ID: 6845034
and most of the times my program takes 0 micro seconds to run? (sometimes 1500 micro seconds.)
0
 
LVL 1

Accepted Solution

by:
madface earned 50 total points
ID: 6845046
so 0 means the application executed really  fast
through  a  in a 6000000 empty loops in your app and see how long it  take
0
 
LVL 1

Expert Comment

by:madface
ID: 6845078
the best way to show you is to break in to the debugger.
wait a while
and execute  the printf statement
it should be a larger number



clock() returns how many ticks  have  elapsed  since you  started  the  app

CLOCKS_PER_SECOND is how many ticks there are in a sec

you get  it
0
 
LVL 1

Expert Comment

by:madface
ID: 6845102
try this  code


void
main()
     {

     long i = 0;

     while(i < 60000000)
          {
          i++;
          }
     
     float cl = clock();
     float val = cl/CLOCKS_PER_SEC;
     printf("cpu= %f",val);

     }
0
 

Expert Comment

by:dchan_4544
ID: 6845175
Actually, clock() reports CPU time USED, which means how much CPU your program actually uses.  So, if your program is not doing much at all, the number you'll get from clock()/CLOCKS_PER_SEC will be very small (very close to 0).  To see a difference, you may be able to do something more complex in a loop like doing bunch of if's or something.  Good luck.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question