Re: Profiling loops



"Christian Christmann" <plfriko@xxxxxxxx> wrote in message
news:pan.2007.03.27.11.16.31.197498@xxxxxxxxxxx
Have you tried using gcov? This measures code coverage, so it will
tell you which lines of code were executed in your program and how
many times. If you need to get some exact counts for specific
conditions in your loop, you could also try making some dummy lines
which do something simple (like incrementing a counter) and then
check
how many times that line was executed.

I think that gcov is not what I'm looking for. With gcov I can
determine
how often a line has been executed in a program run. However, this
profiling information is accumulated, i.e. I would know how often a
loop was executed in total, but I still would not get any
information
on the single number of iterations. Or do I forget something?


Ah, I now see what you mean. Sorry, I did not understand your previous
post exactly.
So reading again, I think you just want the number of iterations spent
in a loop, for a single call each time?

In this case, I do not think gcov (or perhaps any code coverage tools)
will help you here.
As you may have noticed coverage / profiling tools collect data for a
whole execution to allow you to optimise etc... your program, while
what you want is more like "execution tracing" instead.

Unfortunately I cannot think of any solution except to use printfs
which you are already doing. If you find you do this a lot, maybe you
can make some #defines to make it easier to do. Here is a short
example below, however I could not think of a method #define for a for
loop, because you would have to change the code from "for (init;
predicate; increment;) to something like
"FOR(init,predicate,increment)" and even then, it would fail if there
are commas in "init".

#include <stdlib.h>
#include <stdio.h>

#define TRACE(x) printf("Looped %d times in %s at %s:%d\n", x,
__FUNCTION__, __FILE__, __LINE__)
#define CHECK_PRED(pred,var) ((pred) ? true : (TRACE(var),false))
#define APPLY(x,y) x(y)
#define MKVAR(x) var##x
#define VAR APPLY(MKVAR,__LINE__)
#define WHILE(pred) int VAR = 0; while(++VAR, CHECK_PRED(pred, VAR))

int main(void)
{
int nLoop = 0;
WHILE (nLoop < 30)
{
nLoop++;
}
return 0;
}

So, this is as good as I can think of. Anyone else have any ideas??

Mark


.



Relevant Pages

  • Re: I want to learn forth but...
    ... on each pass of the loop. ... deeper in the execution stack. ... faster than even a register bank of any real size. ...
    (comp.lang.forth)
  • Re: Reducing build-times for large projects.
    ... If the line before it executed, then the very next inline code has ... if your architecture does speculative execution and fetch. ... > THE FUNCTION THAT IS ALREADY IN CACHE! ... Loop unrolling "works" because you get to do N iterations worth of the ...
    (comp.lang.cpp)
  • Re: Running a background task
    ... background task that runs more or less forever, while normal execution ... entry On_String (String: in SideAB) ... It's generally not a good idea to reuse the identifier "String", since it hides the predefined type String, which will cause confusing error msgs if you ever try to use that type. ... end loop; ...
    (comp.lang.ada)
  • Re: Python indentation
    ... >>indentation aimlessly wandering out, then in, then out again, then in at ... > It is as much a single flow of execution as an if elif else. ... >>assembly languages have a loop structure quite so ugly as that. ... it's confusing and misleading too. ...
    (comp.lang.python)
  • Re: I want to learn forth but...
    ... on each pass of the loop. ... deeper in the execution stack. ... memory, and the part I quoted gave another loop approach (cyclic ...
    (comp.lang.forth)