#define MacroStringFromClass(class) (@""#class)
#define loopcout 1000000
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loopForMacro];
[self loopForString];
[self loopForNSStringFromClass];
[self loopForMacro];
[self loopForString];
[self loopForNSStringFromClass];
[self loopForMacro];
[self loopForString];
[self loopForNSStringFromClass];
}
- (void)loopForMacro{
NSDate *startDate = [NSDate date];
NSMutableString *str = [NSMutableString string];
for (int i = 0; i < loopcout; i++) {
[str appendString:MacroStringFromClass(ViewController)];
}
NSTimeInterval cost = [[NSDate date]timeIntervalSinceDate:startDate];
NSLog(@"loopForMacro cost:%f",cost);
}
- (void)loopForString{
NSDate *startDate = [NSDate date];
NSMutableString *str = [NSMutableString string];
for (int i = 0; i < loopcout; i++) {
[str appendString:@"ViewController"];
}
NSTimeInterval cost = [[NSDate date]timeIntervalSinceDate:startDate];
NSLog(@"loopForString cost:%f",cost);
}
- (void)loopForNSStringFromClass{
NSDate *startDate = [NSDate date];
NSMutableString *str = [NSMutableString string];
for (int i = 0; i < loopcout; i++) {
[str appendString:NSStringFromClass([ViewController class])];
}
NSTimeInterval cost = [[NSDate date]timeIntervalSinceDate:startDate];
NSLog(@"loopForNSStringFromClass cost:%f",cost);
}
结果:
2016-08-05 15:42:55.804 NSStringFromClassTest[40193:22217987] loopForMacro cost:0.092388
2016-08-05 15:42:55.919 NSStringFromClassTest[40193:22217987] loopForString cost:0.112828
2016-08-05 15:42:56.283 NSStringFromClassTest[40193:22217987] loopForNSStringFromClass cost:0.362415
2016-08-05 15:42:56.362 NSStringFromClassTest[40193:22217987] loopForMacro cost:0.076543
2016-08-05 15:42:56.440 NSStringFromClassTest[40193:22217987] loopForString cost:0.076628
2016-08-05 15:42:56.801 NSStringFromClassTest[40193:22217987] loopForNSStringFromClass cost:0.359302
2016-08-05 15:42:56.877 NSStringFromClassTest[40193:22217987] loopForMacro cost:0.074724
2016-08-05 15:42:56.953 NSStringFromClassTest[40193:22217987] loopForString cost:0.074211
2016-08-05 15:42:57.312 NSStringFromClassTest[40193:22217987] loopForNSStringFromClass cost:0.356701