Solved

clock()

Posted on 2004-03-31
10
520 Views
Last Modified: 2008-03-10
Hi there,

So I wanted to know how long my code takes to run on the order of seconds, so I know about the gprof profiling tool and have used this before.. but for now I just want to use the clock() functions
(And from my experience gprof reports faster times anyways)

So basically I am rendering somethign on screen - and when I just look at it running and count in my head the seconds it takes to run I can count about 7 seconds..
But when I time it using


            clock1 = clock();
            RunOneCardiacCycle( ourMeshExpert, SAMVoxelGrid, timeSampleSet);
            clock2 = clock();
            printf("Time = %.5f\t",(double)(clock2 - clock1) / CLOCKS_PER_SEC);

it reports an average of about 0.6-0.7 seconds!!!  
This makes no sense to me..
I know clock() is not very accurate but I thought it woudl be at least accurate to the order of seconds..?
 Is it because i have many windows open? This can't make such a huge difference!!  Do the above lines of code look right? It shoudl output time in seconds right?
Thanks
0
Comment
Question by:lost_bits1110
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Author Comment

by:lost_bits1110
ID: 10725333
oops., and dont worry about my RunOneCardiacCycle function - thats the function that displays somethign on screen and this is what I'm timing
0
 
LVL 86

Expert Comment

by:jkr
ID: 10725428
Try

DWORD dwBefore, dwAfter;

dwBefore = GetTickCount ();
RunOneCardiacCycle( ourMeshExpert, SAMVoxelGrid, timeSampleSet);
dwAfter = GetTickCount ();
printf("Time = %d ms\t",dwAfter - dwBefore);
0
 

Author Comment

by:lost_bits1110
ID: 10725558
Okay thanks

actually I treid

 time (&start);
 time (&end);
and difftime(end, start)

and this seems to give more realistic values

But now when I use gprof to time it, it also gives me these ridiculous timins that are unreasonable...?
Anything i should be checkign for?


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:shalmak
ID: 10725592
I believe you should use %lf when printing doubles.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 10726809
> But now when I use gprof to time it, it also gives me these ridiculous timins that are unreasonable...?

gprof significantly slows the execution of your program while it gathers run-time statistics.

0
 
LVL 15

Accepted Solution

by:
efn earned 75 total points
ID: 10729242
clock() reports processor time, not elapsed time.  The processor may be doing other things besides running your program, or it may just be idling some of the time waiting for input/output or something.  Your result makes sense if the program is only using the processor 10% of the time.  If you want to measure elapsed time, you should use something else.

--efn
0
 
LVL 1

Expert Comment

by:Lescha
ID: 10731342
Take a look at GetPerformance... functions. They should give you the most exact time measurements available.
0
 

Author Comment

by:lost_bits1110
ID: 10735167
hmm.. okay even if i get rid of the -pg command (ie i dont profile) then its still just as slow

so how can i know if gprof is reporting the correct timings.. i.e. how can i know if my program is using the processor only 10% of time say?
I'm on a redhat linux 9 machine...
Thanks................!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0
 

Author Comment

by:lost_bits1110
ID: 10735792
Hi again..

Okay so by using the
top -d1
command,  i was able to see that my program was using about 25% of the CPU
Also, when I measure the 'elapsed time' I get 6 seconds
When I measure the processor time using gprof, I get 210 milliseconds or 0.21 seconds..

so if I do 6seconds X 0.25%
I get 1.5 seconds..!

This is still about 7 times slower than what gprof reports (0.21 seconds)
WHere is the discreptancy??
0
 

Author Comment

by:lost_bits1110
ID: 10735889
Also note that I compiled and ran my program without using the -pg option i.e. so no run time statistics were being gathered which could slow down the elapsed time, and this also gave me about 6 seconds for the elapsed time
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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.
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.

734 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