Statements time spent 
Author Message
 Statements time spent

Hi,

I need to know how calculate the processor's clocks spent in executing a
set of C instructions (the spent time in ns).

Thanks a lot,

Juan,



Sun, 27 Jun 2004 18:03:02 GMT  
 Statements time spent


Quote:
> Hi,

> I need to know how calculate the processor's clocks spent in executing a
> set of C instructions (the spent time in ns).

You can't.

Quote:
> Thanks a lot,

You're welcome.

See, it's like this. Apart from some boring and tediously simple cases,
C code doesn't correspond directly to the instructions of any particular
machine, and the code that gets generated on some machine varies according
to compiler settings (most obviously, the optimisation flags). What's more,
the same sequence of instructions for the same processor family will take
different numbers of processor ticks depending on details of the machine
architecture, such as cache size and pipelining.

So you can't do it in a system-independant way.

What you *can* do, on most implementations, is ask your C compiler to
show you the generated code, and then get a local expert to analyse
that, and (after they're picked themselves up off the floor after either
hyterical laughter or copious vomiting having seen the quality of the
generated code, which they will do no matter how good it is) tell you
the timings you can expect and the conditions you can expect them in.

Of course, it's possible that's not really what you need to do at all.
Why do you think you need to calculate clock-ticks?

--
Chris "preparing counter-exarmples" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Sun, 27 Jun 2004 19:15:35 GMT  
 Statements time spent



Quote:
>Hi,

>I need to know how calculate the processor's clocks spent in executing a
>set of C instructions (the spent time in ns).

>Thanks a lot,

You can measure CPU time using the clock() standard library function.
The usage is of the form.

#include <time.h>

{
    clock_t starttime, endtime;
    double interval;

    starttime = clock();

    /* The code you want to time goes here */

    endtime = clock();

    interval = (double)(endtime-starttime) / CLOCKS_PER_SEC;

*However* there is no specific resolution guaranteed for clock().
Often it is in the 1/10th to 1/100th second range. There is no portable
way to get a time accurate to ns. Often you can run a sequence of
code many times in a loop to get an interval of time you can measure.

--
-----------------------------------------


-----------------------------------------



Sun, 27 Jun 2004 20:33:01 GMT  
 Statements time spent

Quote:



> > I need to know how calculate the processor's clocks spent in executing a
> > set of C instructions (the spent time in ns).
[...]
> You can't.
[...]
> See, it's like this. Apart from some boring and tediously simple cases,
> C code doesn't correspond directly to the instructions of any particular
> machine, and the code that gets generated on some machine varies according
> to compiler settings (most obviously, the optimisation flags). What's more,
> the same sequence of instructions for the same processor family will take
> different numbers of processor ticks depending on details of the machine
> architecture, such as cache size and pipelining.

> So you can't do it in a system-independant way.

You are absolutely correct.  But Juan is talking about "a _set_ of C
instructions" (which I interpret to mean "a set of C statements"), not
just one C statement.  To simplify the task, we'll just assume that
Juan wants to time how long a function takes to run, because that's a
more common unit of code to time.

To be pedantic, it may be impossible to say how long any function will
take if that function is inlined, in which case its executable code may
be intertwined with another function's code.  However, most C functions
will be in a separate chunk by itself.

Having said all that, the best way to do this is to see if your compiler
supports profiling (search your documentation for "profiling" or
"profiler).  If that's not available, see if there are third party
profilers that support your compiler.  Questions about those are best
asked in a platform-specific newsgroup instead.

Another way to do this is to get the time (see time.h) before you call
the function, and after you call the function.  The difference between
the two times is how long it takes to run your function.  Most platforms
would not support nanosecond-accuracy timing, so you may have to call
your function thousands or millions of times before you get an accurate
timing.

Quote:
> What you *can* do, on most implementations, is ask your C compiler to
> show you the generated code, and then get a local expert to analyse
> that, and (after they're picked themselves up off the floor after either
> hyterical laughter or copious vomiting having seen the quality of the
> generated code, which they will do no matter how good it is) tell you
> the timings you can expect and the conditions you can expect them in.

> Of course, it's possible that's not really what you need to do at all.
> Why do you think you need to calculate clock-ticks?

Other caveats:

1.  Don't assume a function is the bottleneck.  Profile widely and make
    sure first.

2.  Don't profile too early.  You may still make significant changes to
    the code, and invalidate all your results or mislead yourself.

3.  Don't assume that optimizing a single function is the best way to
    improve performance.  Sometimes it's much better to modify the
    _callers_ of that function to not call it so often.



Mon, 28 Jun 2004 03:13:09 GMT  
 Statements time spent

Quote:
> Hi,

> I need to know how calculate the processor's clocks spent in executing a
> set of C instructions (the spent time in ns).

If you want to do it by adding clock count routines to your application,
I'm sure other people could advise you how, but I'd say that if you're
serious about profiling performance, use a profiling tool.

If you're using GNU, you might start with gprof.  If this doesn't give
you what you need (it didn't for me), get a commercial tool - I've used
Quantify from Rational with a great deal of success, but I'm sure there
are other equally good tools on the market.

More later...



Mon, 28 Jun 2004 18:09:43 GMT  
 Statements time spent
You seem to talking about measuring CPU cycles. That's highly
processor-dependent. For the x86 you should read the CPU tick counter
register with the help of the RTSC macro.


Quote:
> Hi,

> I need to know how calculate the processor's clocks spent in executing a
> set of C instructions (the spent time in ns).

> Thanks a lot,

> Juan,




Mon, 28 Jun 2004 23:10:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. STL app spends 25% of time in operator delete(void*)

2. time spent on memory allocation

3. where the program spends its time

4. How to spend idle time in a dialog?

5. Error Messages in <time.h> statement

6. Run time SQL Statement

7. assert statements and Just In Time debugging

8. Bitmaps, again (please help me, I've spent 2 days on this now :-( )

9. Return Statement in Conditional Statement ?.

10. How to precisely count the time spent by a section of code?

11. How to convert local time with specific time zone to UTC time in Win32 API

12. How to convert local time to gmt using a local variable time zone per process/thread

 

 
Powered by phpBB® Forum Software