Solved

clock() not working

Posted on 2002-03-06
11
498 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
Comment Utility
Make it read

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

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

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

Expert Comment

by:madface
Comment Utility
clock()/CLOCKS_PER_SEC just shows how long your your app has been running in seconds
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:madface
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now