unable to accumulate time using gprof - the gnu profiler

强颜欢笑 提交于 2019-12-23 12:35:36

问题


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;}
  }
} 

回答1:


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.




回答2:


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.




回答3:


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

Run gprof --version and tell us the results.



来源:https://stackoverflow.com/questions/1287728/unable-to-accumulate-time-using-gprof-the-gnu-profiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!