Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios iPhone中方法调用的CPU和内存使用情况_Ios_Objective C_Memory_Cpu Usage - Fatal编程技术网

Ios iPhone中方法调用的CPU和内存使用情况

Ios iPhone中方法调用的CPU和内存使用情况,ios,objective-c,memory,cpu-usage,Ios,Objective C,Memory,Cpu Usage,我正在尝试为我的iPhone应用程序的方法调用添加一些性能统计数据。我使用以下方法来寻找处理时间: #define TICK NSDate *startTime = [NSDate date] #define TOCK NSLog(@"Time to process: %f", -[startTime timeIntervalSinceNow]) 是否有类似的策略来测量方法调用的CPU和内存使用情况?使用仪器在实际设备上进行这些测量 NSTimeInterval t1 = [[NSD

我正在尝试为我的iPhone应用程序的方法调用添加一些性能统计数据。我使用以下方法来寻找处理时间:

#define TICK   NSDate *startTime = [NSDate date]
#define TOCK   NSLog(@"Time to process: %f", -[startTime timeIntervalSinceNow])

是否有类似的策略来测量方法调用的CPU和内存使用情况?

使用仪器在实际设备上进行这些测量

 NSTimeInterval t1 = [[NSDate date] timeIntervalSince1970];
。。。。自定义进程

 NSTimeInterval t2 = [[NSDate date] timeIntervalSince1970];
 NSTimeInterval dt = t2-t1; this is in milisec;

使用
仪器
检查应用程序的性能。苹果已经做了相当不错的工作,所以没有必要重新发明轮子。

你需要做一些工作才能让它工作,但下面是你可以做的

  • 因此,您可以了解如何获取当前的CPU使用率
  • 因此,您可以了解如何获取当前内存使用情况
现在,您可以生成一个新线程,定期或按需检查CPU和内存,然后创建一个类,如下所示:

@interface ProfilerBlock
-(id) init;
-(void) end;
@end
-(void) methodToProfile
{
    PROFILE_BLOCK
    // add some code to profile here //
    // the "end" function will get called automatically after the method is done, even if you return early, allowing you to process the profiled data //
}
  • init方法应该初始化当前时间并注册ProfilerBlock实例,以从工作线程获取有关内存使用和CPU使用的信息
  • end方法应该计算时间并打印所有需要的信息或将其写入文件或其他内容:)
现在为ProfilerBlock类创建一个C风格的释放函数

最后,您可以创建宏来简化您的生活:

#define CONCAT2(x, y) x ## y
#define CONCAT(x, y) CONCAT2(x, y)
#define PROFILER_SCOPE_OBJECT __attribute__((cleanup(__$_Profiler_Block_Release_Object_$__)))
#define PROFILE_BLOCK ProfilerBlock *CONCAT(__profilerBlock_, __LINE__) PROFILER_SCOPE_OBJECT = [[ProfilerBlock alloc] init];
完成所有这些之后,您可以像这样分析方法:

@interface ProfilerBlock
-(id) init;
-(void) end;
@end
-(void) methodToProfile
{
    PROFILE_BLOCK
    // add some code to profile here //
    // the "end" function will get called automatically after the method is done, even if you return early, allowing you to process the profiled data //
}

我希望这会有所帮助,如果我没有详细介绍如何测量内存和CPU,我很抱歉,但我相信其他答案中已经很好地介绍了这一点。

连字符在那里做什么?(
…,[startTime timeintervalncesnow])
)否定返回的值。实际上,跨多个环境跟踪大粒度任务的执行时间具有很大的价值。必须使用仪器总是非常禁止的。