xcode 如何测量在函数中花费的总时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23017519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to measure total time spent in a function?
提问by user664939
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.
我有一个实用函数,我怀疑它占用了我的应用程序执行时间的很大一部分。使用 Time Profiler 查看调用堆栈,此函数占用了调用它的任何函数的大部分执行时间。但是,由于此实用程序函数是从许多不同来源调用的,因此我无法确定总体上这是否是优化时间的最佳利用。
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:
为清楚起见,我想将所选条目与对该函数的所有其他调用合并为一个条目:
回答by Alexis Bauchu
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 或 View->Inspectors->Show Display Settings)
回答by SayeedHussain
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.
回答by SimonD
I can offer the makings of the answer you're looking for but haven't got this working within Instruments yet...
我可以提供您正在寻找的答案的材料,但尚未在 Instruments 中使用...
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.
仪器dtrace
在引擎盖下使用。 dtrace
允许您响应程序中的事件,例如输入或返回的函数。可以编写对每个事件的响应的脚本。
You can create a custom instrumentwith scripting in Instruments.
您可以在 Instruments 中使用脚本创建自定义仪器。
Here is a noddy shell script that launches dtrace outside of Instruments and records the time spent in a certain function.
这是一个 noddy shell 脚本,它在 Instruments 之外启动 dtrace 并记录在某个函数中花费的时间。
#!/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.
我还没有想出的是如何将其传输到仪器中并使结果以有用的方式显示在 GUI 中。
回答by SimonD
To see the totals for a particular function, follow these steps:
要查看特定函数的总计,请执行以下步骤:
- Profile your program with Time Profiler
- Find and select any mention of the function of interest in the Call Tree view (you can use Edit->Find)
- 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 )
- 使用 Time Profiler 分析您的程序
- 在“调用树”视图中查找并选择任何提及的感兴趣的函数(您可以使用“编辑”->“查找”)
- 在所选函数上调用上下文菜单和“关注由 发起的调用”(或使用 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.
如果您的程序是多线程的,并且您想要所有线程的总数,请确保未选中“按线程分离”。
回答by Андрей Румянцев
I think you can call system("time ls"); twice and it will just work for you. The output will be printed on debug console.
我想你可以调用 system("time ls"); 两次,它只会为你工作。输出将打印在调试控制台上。