时间
iOS代码执行耗时 单位:ms
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
// do something
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"%f", end - start);
iOS获取当前时间戳 单位:ms
- (NSString *)currentTimeStr{
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval time=[date timeIntervalSince1970]*1000;// 精确到毫秒
NSString *timeString = [NSString stringWithFormat:@"%.00f", time];
return timeString;
}
JS代码执行耗时 单位:ms
console.time("label");
// do something
console.timeEnd("label"); // 输出: do something cost xx ms
JS获取当前时间戳 单位:ms
var timestamp=new Date().getTime();
内存
iOS获取当前应用占用内存 单位:KB
获取iOS当前应用内存占用还有另外一种方式,resident_size。此种方式有时与Instruments看到的存在差异,因此不予提及。
#import<sys/sysctl.h>
#import<mach/mach.h>
+ (double) memoryUsage
{
int64_t memoryUsageInByte = 0;
task_vm_info_data_t vmInfo;
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count);
if(kernelReturn == KERN_SUCCESS) {
memoryUsageInByte = (int64_t) vmInfo.phys_footprint;
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
}
return memoryUsageInByte / 1024;
}
测试工具
BlueEye、XCode Instrument等