1

在某处- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"TagCell";
TagCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                forIndexPath:indexPath];

对比

NSString *CellIdentifier = NSStringFromClass([TagCell class]);
TagCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                forIndexPath:indexPath];

我认为第一个示例的性能更好,但第二个示例更适合重构。

我总是使用第二种方式。

这些代码示例之间的性能差异很大吗?

4

2 回答 2

6
    #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
于 2016-08-05T07:51:19.020 回答
3

两者之间没有明显的性能差异,因为单元格的渲染以绘制为主,而不是获取类名。您的第二个代码片段可以更好地进行重构,因此您应该继续使用它。

于 2013-01-21T23:54:40.173 回答