Re: Profiling loops
- From: "Mark Holland" <kenshin_40@xxxxxxxxxxx>
- Date: Tue, 27 Mar 2007 21:20:44 +0100
"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
.
- Follow-Ups:
- Re: Profiling loops
- From: Christian Christmann
- Re: Profiling loops
- References:
- Profiling loops
- From: Christian Christmann
- Re: Profiling loops
- From: Mark Holland
- Re: Profiling loops
- From: Christian Christmann
- Profiling loops
- Prev by Date: Re: char *** help
- Next by Date: Re: Process Synchronization using Pipes
- Previous by thread: Re: Profiling loops
- Next by thread: Re: Profiling loops
- Index(es):
Relevant Pages
|