- 開(kāi)發(fā)中在很多情況下需要測(cè)試某一個(gè)代碼塊執(zhí)行的時(shí)間,以便提供更好的優(yōu)化方案,以下提供了幾種時(shí)間差值計(jì)算的方案.
dispatch_benchmark
- dispatch_benchmark 是蘋(píng)果GCD內(nèi)部API,能直接返回某一個(gè)代碼塊執(zhí)行多次后的平均每次執(zhí)行時(shí)間,返回時(shí)間單位是ns.
- 使用方法如下:
- 需要提前聲明,否則會(huì)報(bào)錯(cuò),提示私有API無(wú)法直接使用.
// 因?yàn)槭撬接衋pi 所以需要聲明一下 count代表block執(zhí)行的次數(shù)
extern uint64_t dispatch_benchmark(size_t count, void(^bock)(void));
- 聲明后可以直接使用,使用方法如下
// block執(zhí)行次數(shù)
size_t count = 300;
size_t signalCount = 1000;
//benchmarkTime 為執(zhí)行該block count 次后平均每一次的時(shí)間,單位ns
// block內(nèi)部的代碼會(huì)循環(huán)執(zhí)行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是通過(guò)獲取coreAnimation層的絕對(duì)時(shí)間進(jìn)行計(jì)算,單位是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獲取時(shí)間
- 通過(guò)NSDate 的屬性timeIntervalSinceReferenceDate 獲取到參考日期以來(lái)的間隔,然后計(jì)算兩個(gè)間隔之間的差值.單位是s.
NSDate* date = [[NSDate alloc] init];
// 參考日期以來(lái)的間隔
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
- 通過(guò)CFAbsoluteTimeGetCurrent獲取時(shí)間.
- API說(shuō)明
/* absolute time is the time interval since the reference date /
/ the reference date (epoch) is 00:00:00 1 January 2001. */
你會(huì)發(fā)現(xiàn) 其實(shí)和NSDate 原理是一樣的,與參考時(shí)間進(jìn)行計(jì)算.
具體使用如下:
/* absolute time is the time interval since the reference date */
/* the reference date (epoch) is 00:00:00 1 January 2001. */
// 你會(huì)發(fā)現(xiàn) 其實(shí)和NSDate 原理是一樣的,與參考時(shí)間進(jìn)行計(jì)算
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
- 通過(guò)clock獲取時(shí)鐘時(shí)間,同樣適用于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);