Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

clock()

Posted on 2004-03-31
10
Medium Priority
?
523 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 300 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified 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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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