How to measure total time spent in a function?

夙愿已清 提交于 2019-11-28 00:52:12

问题


I have a utility function that I suspect is eating up a large portion of my application's execution time. Using Time Profiler to look at the call stack, this function takes up a large portion of the execution time of any function from which it is called. However, since this utility function is called from many different sources, I am having trouble determining if, overall, this is the best use of my optimization time.

How can I look at total time spent in this function during program execution, regardless of who called it?

For clarity, I want to combine the selected entries with all other calls to that function into a single entry:


回答1:


For me, what does the trick is ticking "Invert Call Tree". It seems to sort "leaf" functions in the call tree in order of those that cumulate the most time, and allow you to see what calls them.

The checkbox can be found in the right panel, called "Display Settings" (If hidden: ⌘2 or View->Inspectors->Show Display Settings)




回答2:


I am not aware of an instruments based solution but here is something you can do from code. Hope somebody provides an instruments solution but until then to get you going here goes.

#include <time.h> 

//have this as a global variable to track time taken by the culprit function
static double time_consumed = 0;

void myTimeConsumingFunction(){
//add these lines in the function
clock_t start, end;

start = clock();
//main body of the function taking up time
end = clock();

//add this at the bottom and keep accumulating time spent across all calls
time_consumed += (double)(end - start) / CLOCKS_PER_SEC;
}

//at termination/end-of-program log time_consumed.



回答3:


To see the totals for a particular function, follow these steps:

  1. Profile your program with Time Profiler
  2. Find and select any mention of the function of interest in the Call Tree view (you can use Edit->Find)
  3. Summon the context menu over the selected function and 'Focus on calls made by ' (Or use Instrument->Call Tree Data Mining->Focus on Calls Made By )

If your program is multi-threaded and you want a total across all threads, make sure 'Separate by Thread' is not checked.




回答4:


I can offer the makings of the answer you're looking for but haven't got this working within Instruments yet...

Instruments uses dtrace under the hood. dtrace allows you to respond to events in your program such as a function being entered or returned from. The response to each event can be scripted.

You can create a custom instrument with scripting in Instruments.

Here is a noddy shell script that launches dtrace outside of Instruments and records the time spent in a certain function.

#!/bin/sh

dtrace -c <yourprogram> -n '

unsigned long long totalTime;
self uint64_t lastEntry;

dtrace:::BEGIN
{
    totalTime = 0;
}

pid$target:<yourprogram>:*<yourfunction>*:entry
{
    self->lastEntry = vtimestamp;
}

pid$target:<yourprogram>:*<yourfunction>*:return
{
    totalTime = totalTime + (vtimestamp - self->lastEntry);
    /*@timeByThread[tid] = sum(vtimestamp - self->lastEntry);*/
}

dtrace:::END
{
    printf( "\n\nTotal time %dms\n" , totalTime/1000000 )
}
'

What I haven't figured out yet is how to transfer this into instruments and get the results to appear in a useful way in the GUI.




回答5:


I think you can call system("time ls"); twice and it will just work for you. The output will be printed on debug console.



来源:https://stackoverflow.com/questions/23017519/how-to-measure-total-time-spent-in-a-function

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