为什么同一段drawrect中的代码,在不同的环境下运行效率不同


先是背景

开发过程中遇到UItableview优化无能的情况,滑动的时候总感觉有点钝。
按照如下的方法,我进行了优化
主要是这两篇文章: http://www.keakon.net/2011/08/03/%E4%...
http://fann.im/blog/2012/09/11/uitabl...
还有这个例程包中的最后一个工程:
http://developer.apple.com/library/io...

在按照这些方法写改以后,确实好了不少。主要是自己写了个cellview,重写了drawrect方法来绘制自己需要定制的内容,而不是给cell上addsubview。但滑动的时候还是有点钝。

下面是问题的核心了~~~

我今天跟如上链接的官网例程做了仔细的对比,发现,把我的drawrect代码 贴到它对应的drawrect中去,效果是不错的,当然cell中要绘制的String都是用@"xyz"这个简单的字符串代替的。

为了控制变量,我把我自己的代码中涉及到展示String数据的部分也改成了@"xyz"。

运行后,计时数据显示,我自己的app框架下,那段代码执行的时间比放在例程中执行的时间要多1/3左右。都是用真机测试的。

这个让我很不解,为什么同样的代码在两个环境下会有不同的效率呢?

我的drawrect方法代码如下:

- (void)drawRect:(CGRect)rect {

    static int i;
    static uint64_t count;
    uint64_t start = mach_absolute_time();

#define LEFT_COLUMN_OFFSET 10
#define LEFT_COLUMN_WIDTH 130

#define MIDDLE_COLUMN_OFFSET 140
#define MIDDLE_COLUMN_WIDTH 110

#define RIGHT_COLUMN_OFFSET 270

#define UPPER_ROW_TOP 8
#define LOWER_ROW_TOP 34

#define MAIN_FONT_SIZE 18
#define MIN_MAIN_FONT_SIZE 16
#define SECONDARY_FONT_SIZE 12
#define MIN_SECONDARY_FONT_SIZE 10

    // Color and font for the main text items (time zone name, time)
    UIColor *mainTextColor = nil;
    UIFont *mainFont = [UIFont systemFontOfSize:MAIN_FONT_SIZE];

    // Color and font for the secondary text items (GMT offset, day)
    UIColor *secondaryTextColor = nil;
    UIFont *secondaryFont = [UIFont systemFontOfSize:SECONDARY_FONT_SIZE];

    mainTextColor = [UIColor blackColor];
    secondaryTextColor = [UIColor colorWithRed:0x9a/255.0 green:0x9a/255.0 blue:0x9a/255.0 alpha:1];


    // Choose font color based on highlighted state.
    if (self.highLighted) {
        [CHYTableViewCellSelectedColor set];
        UIRectFill(self.frame);
    }

    CGPoint point;

    // Set the color for the main text items.
    [mainTextColor set];
    point = CGPointMake(35, 5);
    [self.contactListCellData.name drawAtPoint:point forWidth:150 withFont:mainFont minFontSize:16 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];

    // Set the color for the secondary text items.
    [secondaryTextColor set];

    if (_contactListCellData.subTitle != nil && ![_contactListCellData.subTitle isEqualToString:@""]) {
        point = CGPointMake(36, 28);
        [_contactListCellData.subTitle drawAtPoint:point forWidth:135 withFont:secondaryFont minFontSize:13 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
    }

    if (_contactListCellData.rightLabelStr != nil && ![_contactListCellData.rightLabelStr isEqualToString:@""]) {
        point = CGPointMake(210, 15);
        [_contactListCellData.rightLabelStr drawInRect:CGRectMake(180, 15, 115, 25) withFont:secondaryFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];
    }
    //line
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, self.frame.size.height- 0.25);
    CGContextAddLineToPoint(context, UI_SCREEN_WIDTH, self.frame.size.height- 0.25);
    CGContextClosePath(context);
    [[UIColor colorWithRed:0xaa/255.0 green:0xaa/255.0 blue:0xaa/255.0 alpha:1.0] setStroke];
    CGContextSetLineWidth(context, 0.5);
    CGContextStrokePath(context);


    CGContextMoveToPoint(context, 0, 0.25);
    CGContextAddLineToPoint(context, UI_SCREEN_WIDTH,  0.25);
    CGContextClosePath(context);
    [[UIColor colorWithRed:0xff/255.0 green:0xff/255.0 blue:0xff/255.0 alpha:1.0] setStroke];
    CGContextSetLineWidth(context, 0.5);
    CGContextStrokePath(context);


    uint64_t drawTime = mach_absolute_time() - start;

    i ++;
    count += drawTime;
    if (i > 50) {
        NSLog(@"TEST:%lld [%d] ", count / i, i);
    }
}

ios iphone uitableview

雨夜的憔悴 10 years, 9 months ago

给自己结个题。

问题出在其他部分的代码上,我给tableview所在的viewcontroller的view的CALayer加了shadow的渲染。去掉就好了。
这种calayer层的渲染也很费资源啊。。看来得仔细研究下这些图形渲染机制

东西方不败 answered 10 years, 9 months ago

Your Answer