How to measure total time spent in a function?

你说的曾经没有我的故事 提交于 2019-11-29 06:57:50

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)

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.

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.

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.

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

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