关闭

iPhone用nib/xib文件载入窗口,和用代码写窗口,到底哪个快?

582人阅读 评论(0) 收藏 举报
分类:

有人认为iPhone上用代码来构建加载窗口要比用nib文件来得更快。真的假的?下面文章做了一个实验,说明了这一问题:

简单介绍

这篇文章使用的Sample程序非常简单:一个包含了20行Cell的UITableview,每一行Cell又包含了20个UILabel,1个backgroundView,还有一个selectedBackgroundView。

计时只是在构建和加载的时候进行。像把cell加进UITableView、配置cell、屏幕绘制的过程没有进行计时,因为不管用代码还是用nib,这些过程都会有。



上图右手边,每个cell上的黑线是”placeholder”,被写了19次。

你可以下载工程:工程文件

创建Cell的代码
创建一个cell的代码如下:

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

cell.backgroundView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];

cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

cell.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];


UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 60, 20)] autorelease];

firstLabel.tag = 1;

firstLabel.font = [UIFont boldSystemFontOfSize:14];

firstLabel.shadowOffset = CGSizeMake(1,1);

firstLabel.textColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:1.0];

firstLabel.backgroundColor = [UIColor clearColor];

firstLabel.text = @"placeholder";

firstLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

firstLabel.adjustsFontSizeToFitWidth = YES;

firstLabel.minimumFontSize = 10;

firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

firstLabel.lineBreakMode = UILineBreakModeTailTruncation;

firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

firstLabel.highlightedTextColor = [UIColor clearColor];


[cell addSubview:firstLabel];


// // Plus the construction of a further 19 labels... //

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

cell.backgroundView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];

cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

cell.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];

 

UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 60, 20)] autorelease];

firstLabel.tag = 1;

firstLabel.font = [UIFont boldSystemFontOfSize:14];

firstLabel.shadowOffset = CGSizeMake(1,1);

firstLabel.textColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:1.0];

firstLabel.backgroundColor = [UIColor clearColor];

firstLabel.text = @"placeholder";

firstLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

firstLabel.adjustsFontSizeToFitWidth = YES;

firstLabel.minimumFontSize = 10;

firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

firstLabel.lineBreakMode = UILineBreakModeTailTruncation;

firstLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

firstLabel.highlightedTextColor = [UIColor clearColor];

 

[cell addSubview:firstLabel];

 

// // Plus the construction of a further 19 labels... //


加载nib文件

有很多方法可以从nib文件中加载一个UITableViewCell。这里使用一个最快最简单的方法:

[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];

cell = loadedCell;

loadedCell = nil; 

1

2

3

[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];

cell = loadedCell;

loadedCell = nil; 


模拟器结果

Generated in code

Loaded from NIB

Generated cell in 0.00153798 seconds

Loaded cell in 0.00184 seconds

Generated cell in 0.00138998 seconds

Loaded cell in 0.00168097 seconds

Generated cell in 0.00138199 seconds

Loaded cell in 0.00168198 seconds

Generated cell in 0.00139898 seconds

Loaded cell in 0.001706 seconds

Generated cell in 0.00167602 seconds

Loaded cell in 0.001697 seconds

Generated cell in 0.00235301 seconds

Loaded cell in 0.00171804 seconds

Generated cell in 0.00137097 seconds

Loaded cell in 0.002105 seconds

Generated cell in 0.00138301 seconds

Loaded cell in 0.00173801 seconds

Generated cell in 0.00140399 seconds

Loaded cell in 0.00171405 seconds

Generated cell in 0.00137198 seconds

Loaded cell in 0.001692 seconds

通过nib文件的方式加载比用代码加载慢了将近20%。但是,每次加载只是慢1毫秒左右,关系并不是很大。

真机测试结果
下面这个表是用iPhone 3G测试的:

Generated in code

Loaded from NIB

Generated cell in 0.113011 seconds

Loaded cell in 0.131085 seconds

Generated cell in 0.114312 seconds

Loaded cell in 0.097244 seconds

Generated cell in 0.101614 seconds

Loaded cell in 0.08413 seconds

Generated cell in 0.105022 seconds

Loaded cell in 0.081331 seconds

Generated cell in 0.10087 seconds

Loaded cell in 0.093407 seconds

Generated cell in 0.105968 seconds

Loaded cell in 0.083472 seconds

Generated cell in 0.100045 seconds

Loaded cell in 0.091788 seconds

Generated cell in 0.105458 seconds

Loaded cell in 0.083763 seconds

Generated cell in 0.098836 seconds

Loaded cell in 0.08714 seconds

Generated cell in 0.102028 seconds

Loaded cell in 0.109811 seconds

从上面结果上来看,构建第一个cell时代码的方式要快15%,但是从第三个cell开始nib方式要快17%。

用CPU sampling的工具测试了一下。发现adjustsFontSizeToFitWidth 的方法比较慢。这个方法是Interface Builder用来预先计算字的大小的。我们不要用这个方法。修改代码和nib文件(label.adjustsFontSizeToFitWidth = NO、使用Cell2.xib)后,运行得到下面的结果:

Generated in code

Loaded from NIB

Generated cell in 0.085553 seconds

Loaded cell in 0.095012 seconds

Generated cell in 0.077257 seconds

Loaded cell in 0.087141 seconds

Generated cell in 0.084639 seconds

Loaded cell in 0.082693 seconds

Generated cell in 0.079142 seconds

Loaded cell in 0.098218 seconds

Generated cell in 0.078286 seconds

Loaded cell in 0.082136 seconds

Generated cell in 0.087895 seconds

Loaded cell in 0.087088 seconds

Generated cell in 0.0792 seconds

Loaded cell in 0.082335 seconds

Generated cell in 0.084037 seconds

Loaded cell in 0.082358 seconds

Generated cell in 0.076416 seconds

Loaded cell in 0.08714 seconds

Generated cell in 0.078426 seconds

Loaded cell in 0.084312 seconds

现在,代码方式快了7%。

结论:
不要做出这样的假设,nib文件总是比写代码的方式慢。在一般情况下,使用代码方式生成view比用nib的方式快5%~10%。使用nib文件虽然比较慢,但是差别非常小,没有关系。况且有时候用nib文件要比用代码来得快。

这并不意味着在iphone中UI的速度无关紧要。我曾经写过加载要用2秒的view。这是不能接受的。但是节省10%的nib时间,也不会解决这个问题。在这种情况下,减少view深度,或将text field删除是优化性能的最好方法。这样做能让程序快上10倍或者更多。

用还是不用nib,完全看你的选择。原则就是自己coding舒服并且程序没有性能问题。不要太在意这两个方法的性能差异。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:87142次
    • 积分:1615
    • 等级:
    • 排名:千里之外
    • 原创:73篇
    • 转载:6篇
    • 译文:8篇
    • 评论:15条
    最新评论