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

摘要:
将单元格添加到UITableView、配置单元格和屏幕绘制等过程不定时,因为无论是使用代码还是使用nib,这些过程都会发生。在上图的右侧,每个单元格上的黑线都是“占位符”,已经写了19次。您可以下载项目:创建单元格的项目文件代码如下:Objective-C123456789101112131415161718192021222324cell=[[UITableViewCellalloc]initWithFrame:CGRectZeroreuseIdentifier:CellIdentifier]autorelease];cell.backgroundView=[[UIViewalloc]initWithFrame:CGRectZero]自动释放];cell.backgroundView.backgroundColor=[UIColorWithWhite:0.95阿尔法:1.0];cell.selectedBackgroundView=[[UIViewalloc]initWithFrame:CGRectZero]自动释放];cell.selectedBackgroundView.backgroundColor=[UIColorWithWhite:0.85alpha:1.0];UILabel*firstLabel=[[UILabelloc]initWithFrame:CGRectMake]自动释放];firstLabel.tag=1;firstLabel.font=[UIFontboldSystemFontOfSize:14];firstLabel.shadowOffset=CGSizeMake(1,1);firstLabel.textColor=[UIColorWithRed:0.0green:0.2blue:0.5alpha:1.0];firstLabel.backgroundColor=[UIClorclearColor];firstLabel.text=@“占位符”;firstLabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;firstLabel.adjustsFontSizeToFitWidth=是;firstLabel.minimumFontSize=10;firstLabel.baselineAdjustment=UI基线调整AlignCenters;firstLabel.lineBreakMode=UILineBreakModeTailTruncation;firstLabel.baselineAdjustment=UI基线调整AlignCenters;firstLabel.highlightedTextColor=[UIClorclearColor];[celladdSubview:firstLabel];////加上其他19个标签的结构…//有许多方法可以加载笔尖文件。您可以从nib文件加载UITableViewCell。这里是最快、最容易使用的方法:Objective-C123[[NSBundlemainBundle]loadNibName:@“Cell”owner:self-options:nil];cell=已加载单元格;loadedCell=nil;模拟结果生成代码从NIB生成的单元格加载0.00153798秒加载单元格0.00184秒生成的单元格0.00138998秒加载的单元格0.0016897秒生成的窗口0.00138199秒加载的窗口0.00168198秒生成的控件0.00139898秒加载加载的单元格0.0 01706秒生成的屏幕0.00167602秒加载的屏幕0.001697秒生成的对话框0.0023301秒加载的控件0.0017180秒4seco NdsGeneratedcellin0.00137097秒Loadedcellin0.002105秒Generatedcellin 0.00138301秒Loadedcelin 0.00173801秒Generatedcelin 0.00140399秒LoadedCellin 0.00171405秒Generatedellin 0.00131988秒LoadedCELin 0.001692秒比使用代码加载慢近20%。然而,每次加载仅慢约1毫秒,这并不十分重要。

本篇译自:Matt Gallagher的blog-http://cocoawithlove.com
原文:http://cocoawithlove.com/2010/03/load-from-nib-or-construct-views-in.html

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

简单介绍

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

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

test

Sample Application

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

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

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

Objective-C
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:CGRectZeroreuseIdentifier:CellIdentifier]autorelease];
cell.backgroundView=[[[UIView alloc]initWithFrame:CGRectZero]autorelease];
cell.backgroundView.backgroundColor=[UIColorcolorWithWhite:0.95alpha:1.0];
cell.selectedBackgroundView=[[[UIView alloc]initWithFrame:CGRectZero]autorelease];
cell.selectedBackgroundView.backgroundColor=[UIColorcolorWithWhite:0.85alpha:1.0];
UILabel *firstLabel=[[UILabel alloc]initWithFrame:CGRectMake(5,0,60,20)]autorelease];
firstLabel.tag=1;
firstLabel.font=[UIFontboldSystemFontOfSize:14];
firstLabel.shadowOffset=CGSizeMake(1,1);
firstLabel.textColor=[UIColorcolorWithRed:0.0green:0.2blue:0.5alpha: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];
[celladdSubview:firstLabel];
// // Plus the construction of a further 19 labels... //

加载nib文件

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

Objective-C
1
2
3
[[NSBundle mainBundle]loadNibNamed:@"Cell"owner:selfoptions:nil];
cell=loadedCell;
loadedCell=nil;

模拟器结果

Generated in codeLoaded from NIB
Generated cell in 0.00153798 secondsLoaded cell in 0.00184 seconds
Generated cell in 0.00138998 secondsLoaded cell in 0.00168097 seconds
Generated cell in 0.00138199 secondsLoaded cell in 0.00168198 seconds
Generated cell in 0.00139898 secondsLoaded cell in 0.001706 seconds
Generated cell in 0.00167602 secondsLoaded cell in 0.001697 seconds
Generated cell in 0.00235301 secondsLoaded cell in 0.00171804 seconds
Generated cell in 0.00137097 secondsLoaded cell in 0.002105 seconds
Generated cell in 0.00138301 secondsLoaded cell in 0.00173801 seconds
Generated cell in 0.00140399 secondsLoaded cell in 0.00171405 seconds
Generated cell in 0.00137198 secondsLoaded cell in 0.001692 seconds

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

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

Generated in codeLoaded from NIB
Generated cell in 0.113011 secondsLoaded cell in 0.131085 seconds
Generated cell in 0.114312 secondsLoaded cell in 0.097244 seconds
Generated cell in 0.101614 secondsLoaded cell in 0.08413 seconds
Generated cell in 0.105022 secondsLoaded cell in 0.081331 seconds
Generated cell in 0.10087 secondsLoaded cell in 0.093407 seconds
Generated cell in 0.105968 secondsLoaded cell in 0.083472 seconds
Generated cell in 0.100045 secondsLoaded cell in 0.091788 seconds
Generated cell in 0.105458 secondsLoaded cell in 0.083763 seconds
Generated cell in 0.098836 secondsLoaded cell in 0.08714 seconds
Generated cell in 0.102028 secondsLoaded cell in 0.109811 seconds

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

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

Generated in codeLoaded from NIB
Generated cell in 0.085553 secondsLoaded cell in 0.095012 seconds
Generated cell in 0.077257 secondsLoaded cell in 0.087141 seconds
Generated cell in 0.084639 secondsLoaded cell in 0.082693 seconds
Generated cell in 0.079142 secondsLoaded cell in 0.098218 seconds
Generated cell in 0.078286 secondsLoaded cell in 0.082136 seconds
Generated cell in 0.087895 secondsLoaded cell in 0.087088 seconds
Generated cell in 0.0792 secondsLoaded cell in 0.082335 seconds
Generated cell in 0.084037 secondsLoaded cell in 0.082358 seconds
Generated cell in 0.076416 secondsLoaded cell in 0.08714 seconds
Generated cell in 0.078426 secondsLoaded 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舒服并且程序没有性能问题。不要太在意这两个方法的性能差异。

转 http://www.wangdg.com/?p=485

免责声明:文章转载自《iPhone用nib/xib文件载入窗口,和用代码写窗口,到底哪个快?(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇1.Vue技术栈开发实战-使用vue-cli3创建项目不可错过的javascript迷你库下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JavaWeb项目操作Excel(导出、解析)

对于javaweb开发者来说,对于Excel的操作是必须要了解的知识点,自己原先也做过一些excel的操作,但是没有系统的整理过,所以每回使用的时候都是在重新查找信息,所以做了如下整理: 其实在javaweb开发中经常用到的Excel操作便是导出、解析。下面就从这两个方面来总结。 首先便是导出,在这个方面其实我主要用到的是两种方式:一种是依赖于freema...

iPhone应用开发视频教程斯坦福大学公开课

以下是一套由美国斯坦福大学(Stanford University)在2013年初推出的一套iPhone应用程序开发视频教程,详细讲解了iPhone4/iPhone4s/iPhone5/iPad等iOS平台终端上app应用程序的开发。 由浅入深,通过从最基本的app store产业形态讲起,让开发者提前了解应用从构想、设计、开发、上架、运营的完整...

iOS获取所有机型

1.手机系统版本:10.3 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; 2.手机类型:iPhone 6 NSString* phoneModel = [self iphoneType];//方法在下面 3.手机系统:iPhone OS NSString * ipone...

itouch/iphone/ipad充不上电的解决办法

  itouch/iphone/ipad长时间没有用,或是没电自己关机了,再充电开机变白苹果了,网上的方法用遍了都不管用,今天找到一个,很有效,特此分享给大家,具体操作如下:   首先把itouch/iphone/ipad连接至电脑,在出现白苹果的时候,按住电源键和Home键(主菜单键),5秒左右,就会黑屏。然后立刻按住电源键3秒,接下来不松开电源键的同时...

4006701855苹果开发者热线停止使用后如何联系苹果客服入口介绍

随着6月5日一年一度的苹果 WWDC 开发者大会的召开。苹果更新的很多东西,苹果已经永久暂停4006701855中文开发者电话支持, 你打过去语音提示已更改联系方式。 从此遇到问题不能打4006701855这个电话去咨询了。 不过苹果那边还是有咨询服务,改了规则,以前是要主动打电话,现在可以被动要求,苹果客服打电话给你。 那怎么获得苹果开发者咨询服务呢...

HomeKit开发(一)

需求:因为公司的生产硬件需要进入苹果的HomeKit市场,因此需要一款供苹果审核的APP(功能:苹果的家庭软件的功能+公司的硬件支持的功能特有功能)需求驱动开发:最近做了一款苹果HomeKit的软件 这一篇主要介绍苹果定义的智能家居的框架(HomeKit框架),这些东西都是苹果的官方文档里面的东西,在这里是系统的总结,以便于更好的理解该框架!!! #pra...