Solved

clock()

Posted on 2004-03-31
10
517 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
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.

773 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