今天看到一篇关于iOS应用性能优化的文章,其中提到计算代码的运行时间,觉得非常有用,值得收藏。不过在模拟器和真机上是有差异的,以此方法观察程序运行状态,提高效率。

第一种:(最简单的NSDate)

NSDate* tmpStartData = [NSDate date];
//You code here...
double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];
NSLog(@"cost time = %f", deltaTime);

第二种:(将运行代码放入下面的Block中,返回时间)

#import <mach/mach_time.h>  // for mach_absolute_time() and friends
CGFloat BNRTimeBlock (void (^block)(void)) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
uint64_t start = mach_absolute_time ();
block ();
uint64_t end = mach_absolute_time ();
uint64_t elapsed = end - start;
uint64_t nanos = elapsed * info.numer / info.denom;
return (CGFloat)nanos / NSEC_PER_SEC;
}

第三种:

/**
* 计算脚本时间
* @param $last 最后一次的运行clock
* @param $key 标识
* @return 当前clock
*/
double t(double last, char* key){
clock_t now = clock();
printf("time:%fs \t key:%s \n", (last != 0) ? (double)(now - last) / CLOCKS_PER_SEC : 0, key);
return now;
}
double t1 = t(0, "");
//do something
t(t1, "end");

飘飘白云文中说到,对于UIImage载入图像的方法,下面第一种更为高效,因为iOS会自带 cache 载入图像。

+ (UIImage *)imageNamed:(NSString *)name;
- (id)initWithContentsOfFile:(NSString *)path;

但是,imageNamed方法只能载入bundle中的文件,如网络图像解析就无能为力了。

正好这里总结到计算代码运行时间,就以此为例看看两种方法到底谁更高效,这里我用最简单的NSDate计算耗时:

    for (int i=0 ; i<10000; i++) {
UIImage *image = [UIImage imageNamed:@"icon.png"];
// UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"icon.png"];
image = nil;
}
double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];
NSLog(@"cost time = %f", deltaTime);

输出结果:

cost time = 0.022821 //imageNamed
cost time = 0.102620 //initWithContentsOfFile

这里图片我用的一张1024x1024像素的png,可见两种加载图像的方法耗时差别还是挺大的。
部分引用自:

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

Objective-C 计算代码运行时间的更多相关文章

  1. 使用console进行 性能测试 和 计算代码运行时间(转载)

    本文转载自: 使用console进行 性能测试 和 计算代码运行时间

  2. php计算代码运行时间与内存使用的一段代码

    计算运行时间及内存使用,代码如下: <?php //开始计时 $HeaderTime = microtime(true);//参数true表示返回浮点数值 //代码 //... printf(& ...

  3. 使用console进行 性能测试 和 计算代码运行时间

    原文:http://www.tuicool.com/articles/JrARVjv 对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用 debugger 会显得过于笨重,最常 ...

  4. 使用console进行性能测试和计算代码运行时间

    对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用debugger会显得过于笨重,最常用的方法是会将值输出到控制台上方便调试.最常用的语句就是console.log(expre ...

  5. 使用Objective-C 计算代码运行时间

    第一种:(最简单的NSDate) NSDate* tmpStartData = [NSDate date]; //You code here... double deltaTime = [[NSDat ...

  6. C# 计算代码运行时间

    Stopwatch watch = new Stopwatch(); watch.Start(); int num = myhelper.MySQLExecuteNonQuery(comlist); ...

  7. python计算代码运行时间的装饰器

    import time def cal_time(func): def wrapper(*args, **kwargs): t1 = time.time() result = func(*args, ...

  8. C语言--计算代码段运行时间

    c语言中有专一包含计算时间函数的头文件,time.h.当我们需要计算某段程序运行的时间时就需要用到time.h包含的clock()函数,在这里介绍一下如何使用这个函数计算代码运行时间. clock函数 ...

  9. 计算Python代码运行时间长度方法

    在代码中有时要计算某部分代码运行时间,便于分析. import time start = time.clock() run_function() end = time.clock() print st ...

随机推荐

  1. uC/OS-II内核架构解析(2)---uC/OS-II基本介绍(转)

    1. uC/OS-II文件结构 2. uC/OS-II组成部分 uC/OS-II大致可以分成系统核心(包含任务调度).任务管理.时间管理.多任务同步与通信.内存管理.CPU移植等部分. (1) 核心部 ...

  2. 完美解决android软键盘监听

    最近在做应用性能调优,发现在一个包含有输入框的Activity中,当软键盘弹出的时候,如果直接finish掉此Activity,那么在返回到上一个Activity时,界面的渲染会由于软键盘没有及时的收 ...

  3. linux下查看和设置软件的安装路径

    1:你可以通过whereis 软件名来查找系统里的文件位置 比如你想查找eclipse文件,那么就: [root@localhost ~]# whereis eclipse 会显示: eclipse: ...

  4. linux系统时间和硬件时钟问题(date和hwclock)

    http://blog.chinaunix.net/uid-182041-id-3464524.html http://blog.csdn.net/duyiwuer2009/article/detai ...

  5. 计算机语言学习导论[C/C++]

    作者:@幻の上帝 1 前置条件语文其实挺重要,这个没问题,但容易被忽视.当然,如果不是经常要折腾文档,要求不高:但起码要能说清楚话.数学重要,主要是广度,作为快速学习相关领域知识的基础.深度上面可深可 ...

  6. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 56  Solved: 16[S ...

  7. cf493D Vasya and Chess

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. CoreData Multiple Context性能分析-读书笔记

    From: http://floriankugler.com/blog/2013/4/29/concurrent-core-data-stack-performance-shootout  http: ...

  9. 执行testng appium用例失败,自动截图

    新建一个截图监听类ScreenShotListener ,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package com.fsssc.htsgl.ut ...

  10. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 解决办法

    Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (d ...