Solved

clock()

Posted on 2004-03-31
10
519 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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