clock() not working

#include <time.h>

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

output: cpu= 0
anotherctoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Make it read

printf("cpu= %f",((double) clock())/CLOCKS_PER_SEC);
0
jkrCommented:
FYI, the way you did it, you're doing an integer division. So, if clock() < CLOCKS_PER_SEC, the result is '0'...
0
anotherctoAuthor Commented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

madfaceCommented:
clock()/CLOCKS_PER_SEC just shows how long your your app has been running in seconds
0
madfaceCommented:
clock()/CLOCKS_PER_SEC just shows how long your your app has been running in seconds
0
madfaceCommented:
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
anotherctoAuthor Commented:
and most of the times my program takes 0 micro seconds to run? (sometimes 1500 micro seconds.)
0
madfaceCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
madfaceCommented:
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
madfaceCommented:
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
dchan_4544Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.