Profiling loops
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Profiling loops




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Profiling loops  
Christian Christmann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-26-07 01:20 PM

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





[ Post a follow-up to this message ]



    Re: Profiling loops  
Mark Holland


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-27-07 12:32 AM

> 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







[ Post a follow-up to this message ]



    Re: Profiling loops  
Christian Christmann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-27-07 12:16 PM

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?







[ Post a follow-up to this message ]



    Re: Profiling loops  
Mark Holland


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-28-07 12:26 AM

"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







[ Post a follow-up to this message ]



    Re: Profiling loops  
Christian Christmann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-28-07 12:18 PM

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.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:52 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register