views:

649

answers:

3

I am running cygwin on windows and using latest version of gprof for profiling my code. My problem is that the flat profile shows zero sec for each of the functions in my code, I even tried to loop the functions(tried a for loop for a million) but gprof is unable to accumulate any time .Please help . Here is one of my sample function.

bool is_adjacent(const char* a ,const char* b)
{
  for(long long iter=0;iter<=1000000;iter++){
  string line1="qwertyuiop";
  string line2="asdfghjkl";
  string line3="zxcvbnm";
  string line4="1234567890";
  int pos=line1.find(*a);
  if(pos!=string::npos){
    if ((line1[pos++]==*b)||((pos!=0)&&(line1[pos--]==*b)))
      return true;
    else return false;}
  pos=line2.find(*a);  
  if(pos!=string::npos){
    if ((line2[pos++]==*b)||((pos!=0)&&(line2[pos--]==*b)))
      return true;
    else return false;}
  pos=line3.find(*a);  
  if(pos!=string::npos){
    if ((line3[pos++]==*b)||((pos!=0)&&(line3[pos--]==*b)))
      return true;
    else return false;}
  pos=line4.find(*a);  
  if(pos!=string::npos){
    if ((line4[pos++]==*b)||((pos!=0)&&(line4[pos--]==*b)))
      return true;
    else return false;}
  }
}
A: 

What version of gprof are you using? Some old versions have this exact bug.

Run gprof --version and tell us the results.

teambob
2.18.50.20080625
+1  A: 

If your overall goal is to find and remove performance problems, you might consider this.

I suspect it will show that essentially 100% of the CPU time is being spent in find and string-compare, leaving almost 0% for your code. That's what happens when only the program counter is sampled.

If you sample the call stack, you will see that the lines of code that invoke find and string-compare will be displayed on the stack with a frequency equal to the time they are responsible for.

That's the glory of gprof.

P.S. You could also figure this out by single-stepping the code at the disassembly level.

Mike Dunlavey
A: 

I'm having that problem from time to time. Esp. in heavily threaded code.

You can use valgrind with the --callgrind option (tool) which will allow you to at least have a more detailed view of how much time per function call. There's a kde tool as well to visualize the output (and eswp. callgraph) better called kcachegrind. Don't know if you can install that on cygwin though.

count0