For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2007 > Profiling loops









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Profiling loops
Christian Christmann

2007-03-26, 4:07 am

Hi,

I'm looking for a sort of profiler that provides information about
the number of loop iterations. What I'm interested in is how often
a loop in a given (C) program was executed. Since the loop might be
traversed with different number of iterations (if the induction
increment value or the condition variables are not constant), I need
all these numbers, or at least the minimal and maximal number of
loop iterations.

Up to now, I was adding some debug code to my original program which
counted how often a loop was iterated and after leaving the loop
issued a printf. But this becomes quite cumbersome for larger code.

Do you know of any approach to automatize this profiling?
Maybe any gcc options or other gnu binutils that might do this job?

Thank you.

Regars,
Christian
Mark Holland

2007-03-26, 7:05 pm

> I'm looking for a sort of profiler that provides information about
> the number of loop iterations. What I'm interested in is how often
> a loop in a given (C) program was executed. Since the loop might be
> traversed with different number of iterations (if the induction
> increment value or the condition variables are not constant), I need
> all these numbers, or at least the minimal and maximal number of
> loop iterations.
>
> Up to now, I was adding some debug code to my original program which
> counted how often a loop was iterated and after leaving the loop
> issued a printf. But this becomes quite cumbersome for larger code.
>
> Do you know of any approach to automatize this profiling?
> Maybe any gcc options or other gnu binutils that might do this job?
>


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.

Here is a link from the GCC manual I found through google:
http://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_8.html

HTH

Mark


Christian Christmann

2007-03-27, 8:15 am

On Mon, 26 Mar 2007 19:59:34 +0100, Mark Holland wrote:

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


Mark Holland

2007-03-27, 10:08 pm

"Christian Christmann" <plfriko@yahoo.de> wrote in message
news:pan.2007.03.27.11.16.31.197498@yahoo.de...
>
> 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


Christian Christmann

2007-03-28, 8:15 am

On Tue, 27 Mar 2007 21:20:44 +0100, Mark Holland wrote:

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


Exactly, this is what I need.
>
> #include <stdlib.h>
> #include <stdio.h>

[...]

Thank you, that's in general the same I do. But as you can imagine
this might become very tedious when the program gets complex and
contains many loops.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com