Solved

clock()

Posted on 2004-03-31
10
514 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
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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.

759 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

18 Experts available now in Live!

Get 1:1 Help Now