- 开发中在很多情况下需要测试某一个代码块执行的时间,以便提供更好的优化方案,以下提供了几种时间差值计算的方案.
dispatch_benchmark
- dispatch_benchmark 是苹果GCD内部API,能直接返回某一个代码块执行多次后的平均每次执行时间,返回时间单位是ns.
- 使用方法如下:
- 需要提前声明,否则会报错,提示私有API无法直接使用.
// 因为是私有api 所以需要声明一下 count代表block执行的次数
extern uint64_t dispatch_benchmark(size_t count, void(^bock)(void));
- 声明后可以直接使用,使用方法如下
// block执行次数
size_t count = 300;
size_t signalCount = 1000;
//benchmarkTime 为执行该block count 次后平均每一次的时间,单位ns
// block内部的代码会循环执行count * signalCount 次
uint64_t benchmarkTime = dispatch_benchmark(count, ^{
for (int j = 0; j < 1000; j++) {
@autoreleasepool {
NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < signalCount; ++i) {
[array addObject:@1];
}
}
}
});
// 单位ns
/*
1秒=1000毫秒(ms),1毫秒=1/1000秒(s);
1秒=1000000 微秒(μs),1微秒=1/1000000秒(s);
1秒=1000000000 纳秒(ns),1纳秒=1/1000000000秒(s);
1秒=1000000000000皮秒 1皮秒==1/1000000000000秒.
*/
// benchmark time is = 29739487, is 0.029739 secound
NSLog(@"benchmark time is = %llu, is %f secound",benchmarkTime,benchmarkTime*1.0/1000000000);
CACurrentMediaTime
/* Returns the current CoreAnimation absolute time. This is the result of
* calling mach_absolute_time () and converting the units to seconds. */
- CACurrentMediaTime是通过获取coreAnimation层的绝对时间进行计算,单位是s.
- 使用方法如下:
CFTimeInterval startTimeInterval = CACurrentMediaTime();
for (int i = 0; i < 1000; ++i) {
@autoreleasepool{
NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < 1000; ++i) {
[array addObject:@1];
}
}
}
CFTimeInterval endTimeInterval = CACurrentMediaTime();
//endTimeInterval - startTimeInterval = 0.028688
NSLog(@"endTimeInterval - startTimeInterval = %f",endTimeInterval - startTimeInterval);
NSDate获取时间
- 通过NSDate 的属性timeIntervalSinceReferenceDate 获取到参考日期以来的间隔,然后计算两个间隔之间的差值.单位是s.
NSDate* date = [[NSDate alloc] init];
// 参考日期以来的间隔
NSTimeInterval startTime = [date timeIntervalSinceReferenceDate];
for (int i = 0; i < 1000; ++i) {
@autoreleasepool{
NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < 1000; ++i) {
[array addObject:@1];
}
}
}
NSTimeInterval endTime = [[NSDate date] timeIntervalSinceReferenceDate];
// endTime - startTime is = 0.029718 单位s
NSLog(@"endTime - startTime is = %f",endTime - startTime);
CFAbsoluteTime
- 通过CFAbsoluteTimeGetCurrent获取时间.
- API说明
/* absolute time is the time interval since the reference date /
/ the reference date (epoch) is 00:00:00 1 January 2001. */
你会发现 其实和NSDate 原理是一样的,与参考时间进行计算.
具体使用如下:
/* absolute time is the time interval since the reference date */
/* the reference date (epoch) is 00:00:00 1 January 2001. */
// 你会发现 其实和NSDate 原理是一样的,与参考时间进行计算
CFAbsoluteTime absoluteTimeStart = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < 1000; ++i) {
@autoreleasepool{
NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < 1000; ++i) {
[array addObject:@1];
}
}
}
CFAbsoluteTime absoluteTimeEnd = CFAbsoluteTimeGetCurrent();
// absoluteTimeEnd - absoluteTimeStart = 0.028272 单位s
NSLog(@"absoluteTimeEnd - absoluteTimeStart = %f",absoluteTimeEnd - absoluteTimeStart);
clock_t
- 通过clock获取时钟时间,同样适用于c++,单位us
- 使用方法:
clock_t startTime = clock();
for (int i = 0; i < 1000; ++i) {
@autoreleasepool{
NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < 1000; ++i) {
[array addObject:@1];
}
}
}
clock_t endTime = clock();
//endTime - startTime is = 28534, is 0.028534 second
NSLog(@"endTime - startTime is = %lu, is %f second",endTime - startTime,(endTime - startTime)*1.0/CLOCKS_PER_SEC);