标签:
//创建一个.h 文件
// Method.h
#ifndef NonAtomicTest_Method_h
#define NonAtomicTest_Method_h
#import <mach/mach_time.h>
//c函数实现 返回值类型 函数名 函数参数(是一个block)
CGFloat BNRTimeBlock (void (^block)(void)) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
uint64_t start = mach_absolute_time ();
block ();
uint64_t end = mach_absolute_time ();
uint64_t elapsed = end - start;
uint64_t nanos = elapsed * info.numer / info.denom;
return (CGFloat)nanos / NSEC_PER_SEC;
} // BNRTimeBlock
#endif
//直接使用
#import "ViewController.h"
#import "Method.h" //引用头文件即可调用 BNRTimeBlock
#define LOOPAGE 10000000
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self test];
}
-(void)test {
CGFloat time;
NSString *thing1 = @"hi";
NSString *thing2 = @"hello there";
//block 的实现
time = BNRTimeBlock(^{
for (int i = 0; i < LOOPAGE; i++) {
[thing1 isEqual: thing2];
}
});
NSLog (@"isEqual: time: %f\n", time);
time = BNRTimeBlock(^{
for (int i = 0; i < LOOPAGE; i++) {
[thing1 isEqualToString: thing2];
}
});
NSLog(@"isEqualToString: time: %f\n", time);
}
@end
控制台输出:
NonAtomicTest[3114:126739] isEqual: time: 0.279763
NonAtomicTest[3114:126739] isEqualToString: time: 0.275132
标签:
原文地址:http://www.cnblogs.com/yaoyao0110/p/4813136.html