2017年6月13日

前言

前几日在改Bug时看到好多调试时用的日志语句都被一个日志开关控制着它的执行权。形如:

1 if(Constants.LOG_TAG){2 Log.d(TAG, "Initialize finish! index:" + idx);3 }

又为了方便日后遇到问题时的分析,还要加上很多类似的日志语句。这时突然很好奇“大量的”条件判断语句需要多消耗多少系统资源,以及不同的条件表达式占用系统资源的差异情况。为此,特意设计了一个简单的小实验,通过取多组不同环境下这几种情形的耗时均值作为考量依据。

设计实验

对指定的条件语句进行一亿次的运算,计算运算前后的时间差作为结果。

一亿次循环运算空跑的耗时情况;

if语句条件表达式为简单运算的情况的耗时情况;

if语句条件表达式为局部boolean且值为true;

if语句条件表达式为局部boolean且值为false;

if语句条件表达式为局部final boolean且值为false;

if语句条件表达式为全局boolean且值为false;

if语句条件表达式为全局final boolean且值为false;

if语句条件表达式为另一个类对象中的实例变量boolean且值为false;

if语句条件表达式为另一个类对象中的静态变量boolean且值为true;

if语句条件表达式为另一个类对象中的静态变量boolean且值为false;

if语句条件表达式为另一个类对象中的静态final布尔变量且值为true;

if语句条件表达式为另一个类对象中的静态final布尔变量且值为false;

if语句条件表达式为静态final变量boolean且值为false;

if语句条件表达式为静态变量boolean且值为false;

实验环境

操作系统:Android 5.1

设备系统:4K智能电视

处理器:Hi3751v620

内存:2GB

代码设计

MainActivity.java 主要代码

1 private final String TAG = "chorm";2

3 private final int chorm = 100000000;//1亿

4 private booleanfieldBool;5 private final boolean fieldBool2 = false;6 private static boolean B3 = false;7 private final static boolean B4 = false;8

9 private longgetTime() {10 returnSystemClock.uptimeMillis();11 }12

13 public voidclick(View v) {14 intcounter;15

16 Log.i(TAG, "- Prebuilt -");17 counter =chorm;18 long start =getTime();19 do{20 counter--;21 } while (counter > 0);22 long end =getTime();23

24 Log.i(TAG, " - Demo begin -");25 //1. 一亿次循环运算空跑的耗时情况;

26 counter =chorm;27 start =getTime();28 do{29 counter--;30 } while (counter > 0);31 end =getTime();32 Log.d(TAG, "1:Time spent:" + (end - start) + "ms");33

34 //2. if语句条件表达式为简单运算的情况的耗时情况;

35 counter =chorm;36 start =getTime();37 do{38 counter--;39 if ((9 + 17) == 8) {40

41 }42 } while (counter > 0);43 end =getTime();44 Log.d(TAG, "2:Time spent:" + (end - start) + "ms");45

46 //3. if语句条件表达式为局部boolean且值为true;

47 counter =chorm;48 boolean localBool = true;49 start =getTime();50 do{51 counter--;52 if(localBool) {53

54 }55 } while (counter > 0);56 end =getTime();57 Log.d(TAG, "3:Time spent:" + (end - start) + "ms");58

59 //4. if语句条件表达式为局部boolean且值为false;

60 counter =chorm;61 localBool = false;62 start =getTime();63 do{64 counter--;65 if(localBool) {66

67 }68 } while (counter > 0);69 end =getTime();70 Log.d(TAG, "4:Time spent:" + (end - start) + "ms");71

72 //5. if语句条件表达式为局部final boolean且值为false;

73 counter =chorm;74 final boolean localBool2 = false;75 start =getTime();76 do{77 counter--;78 if(localBool2) {79

80 }81 } while (counter > 0);82 end =getTime();83 Log.d(TAG, "5:Time spent:" + (end - start) + "ms");84

85 //6. if语句条件表达式为全局boolean且值为false;

86 counter =chorm;87 start =getTime();88 do{89 counter--;90 if(fieldBool) {91

92 }93 } while (counter > 0);94 end =getTime();95 Log.d(TAG, "6:Time spent:" + (end - start) + "ms");96

97 //7. if语句条件表达式为全局final boolean且值为false;

98 counter =chorm;99 start =getTime();100 do{101 counter--;102 if(fieldBool2) {103

104 }105 } while (counter > 0);106 end =getTime();107 Log.d(TAG, "7:Time spent:" + (end - start) + "ms");108

109 //8. if语句条件表达式为另一个类对象中的实例变量boolean且值为false;

110 counter =chorm;111 AnotherClass ac = newAnotherClass();112 start =getTime();113 do{114 counter--;115 if(ac.bool) {116

117 }118 } while (counter > 0);119 end =getTime();120 Log.d(TAG, "8:Time spent:" + (end - start) + "ms");121

122 //9. if语句条件表达式为另一个类对象中的静态变量boolean且值为true;

123 counter =chorm;124 start =getTime();125 do{126 counter--;127 if(AnotherClass.BOOL) {128

129 }130 } while (counter > 0);131 end =getTime();132 Log.d(TAG, "9:Time spent:" + (end - start) + "ms");133

134 //10. if语句条件表达式为另一个类对象中的静态变量boolean且值为false;

135 counter =chorm;136 start =getTime();137 do{138 counter--;139 if(AnotherClass.BOOL2) {140

141 }142 } while (counter > 0);143 end =getTime();144 Log.d(TAG, "10:Time spent:" + (end - start) + "ms");145

146 //11. if语句条件表达式为另一个类对象中的静态final变量boolean且值为true;

147 counter =chorm;148 start =getTime();149 do{150 counter--;151 if(AnotherClass.BOOL3) {152

153 }154 } while (counter > 0);155 end =getTime();156 Log.d(TAG, "11:Time spent:" + (end - start) + "ms");157

158 //12. if语句条件表达式为另一个类对象中的静态final变量boolean且值为false;

159 counter =chorm;160 start =getTime();161 do{162 counter--;163 if(AnotherClass.BOOL4) {164

165 }166 } while (counter > 0);167 end =getTime();168 Log.d(TAG, "12:Time spent:" + (end - start) + "ms");169

170 //13. if语句条件表达式为静态final变量boolean且值为false;

171 counter =chorm;172 start =getTime();173 do{174 counter--;175 if(B4) {176

177 }178 } while (counter > 0);179 end =getTime();180 Log.d(TAG, "13:Time spent:" + (end - start) + "ms");181

182 //14. if语句条件表达式为静态变量boolean且值为false;

183 counter =chorm;184 start =getTime();185 do{186 counter--;187 if(B3) {188

189 }190 } while (counter > 0);191 end =getTime();192 Log.d(TAG, "14:Time spent:" + (end - start) + "ms");193

194 Log.v(TAG, " - Demo end -");195 }

AnotherClass.java

1 public classAnotherClass {2 public static boolean BOOL = true;3 public static boolean BOOL2 = false;4 public static final boolean BOOL3 = true;5 public static final boolean BOOL4 = false;6 public booleanbool;7 }

实验结果

共进行了五个不同环境下的测试实验。

第一组:开机后在首页

第二组:打开了很多应用以后

第三组:播放数字电视节目时启动测试应用。

第四组:播放一段4K视频时启动测试应用。

第五组:在首页启动测试应用。

第六组:在首页启动测试应用。

单位:毫秒(ms)

序号

第一组

第二组

第三组

第四组

第五组

第六组

耗时均值

1

347

344

348

346

344

344

345.5

2

347

359

347

344

344

345

347.67

3

346

348

346

345

344

344

345.5

4

346

347

345

344

344

344

345

5

347

346

346

344

344

345

345.17

6

347

346

345

345

344

344

345.17

7

346

346

346

344

344

344

345

8

347

346

345

344

344

345

345.17

9

2292

2277

2279

2268

2265

2264

2274.17

10

1410

1411

1408

1411

1411

1411

1410.33

11

345

345

344

345

344

344

344.5

12

345

345

344

346

363

344

347.83

13

346

345

350

345

345

345

346

14

344

345

350

344

344

344

345.17

结论

对于if语句条件表达式的耗时应以条件结果为false的为准。

大多数情况下条件判断所耗时间相当。

当条件被判断为true时(对比3、4),即时后面的方法体是空的,也需要更多的时间来执行,对比9、10的结果更明显。

非必须条件下不要在条件判断语句处作运算(结果2),尽可能地将运算过程放在编译时来执行。

变量是否被final修饰似乎对结果没有什么影响(4、5与6、7),事实上这个结果让我也很迷惑。不排除是我的测试环境与测试方法的问题。

变量定义在哪一个对象对结果没有什么影响(结果6、结果8 )

对于另一个类对象中的静态变量,所花费的时间相较于其它情况长很多(结果9、结果10)。尽量不要使用其它类对象中的非final静态变量。

我相信没什么应用的调试日志数量能达到1亿条或者更多。即使是1410.33毫秒,分成1亿份,它的时间也是微不足道的,并且市面上移动设备处理器通常都比我手里这块要好。因此,实际结论是:你想怎样用就怎样用。当然,日志打印的耗时情况就不在我们的讨论范围之内了。

android 结束if循环_简单探究Android平台下' if ' 语句条件判断耗时情况相关推荐

  1. android 结束if循环_(第五讲)if 分支语句和 while 循环

    本文首发于微信公众号「烽哥带你学Android」,工作日每日更新 1x0 switch 分支语句 昨天我们最后学习了分支结构中的 if 条件语句.其实和 if 条件语句还有个兄弟 -- switch ...

  2. android 结束if循环_几款Android反编译器对循环结构的还原能力测试记录

    本帖最后由 gjden 于 2019-6-26 14:35 编辑 几款Android反编译器对循环结构的还原能力测试记录 0.motivation 喜欢jadx的人会常常吐槽JEB反编译器:卖的这么贵 ...

  3. oracle sql循环判断语句怎么写,Oracle 非常详细的 PL/SQL入门教程,PL/SQL语法格式/循环语句/条件判断/异常处理...

    PL/SQL入门教程目录 Oracle PL/SQL入门教程,PL/SQL语法格式/循环语句/条件判断/异常处理 一.PL/SQL简介 1.PL/SQL简介 1.PL/SQl是过程语言PL与结构化语言 ...

  4. android studio button位置_免费的Android开发环境

    Android Studio for mac是一个全面的Android开发环境,Android Studio帮助您设计,构建,测试和调试Android应用程序,利用Android Studio所有特色 ...

  5. android开发使用c+_如何在Android项目中开始使用C ++代码

    android开发使用c+ by Onur Tuna 通过Onur Tuna 如何在Android项目中开始使用C ++代码 (How to start using C++ code in your ...

  6. python中continue只结束本次循环_循环(while,break,continue),转义字符

    Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待 4999元包邮 去购买 > 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 - ...

  7. android 控件 堆叠_每次在Android上正确地向后堆叠

    android 控件 堆叠 When navigating in a mobile app, the screens opened after one another form a stack, th ...

  8. android auto 能微信_想使用Android Auto系统你需要知道的11件事

    如果你想要在自己的汽车里使用Android Auto,但是又不知道从何做起.没关系,最近科技网站greenbot专门为我们详细介绍了使用Android Auto系统时应该知道的几件事,包括软件的内部运 ...

  9. android 启动白屏_为什么说Android 架构的未来是 MVVM?

    据<第45次中国互联网络发展状况统计报告>,2019年市场上监测到的APP数量比2018年减少85万款- 这两年,很多朋友都会有这样的疑惑: "现在Android的坑还值不值得入 ...

最新文章

  1. 阅读Book: MultiObjective using Evolutionary Algorithms (6) ---- 满足Pareto-optimality的条件证明,第二章完结
  2. python requests 爬取数据
  3. python函数编程训练题_Python文件与函数练习题
  4. 2021年,腾讯研发人员增长41%,Go首次超越C++成为最热门语言
  5. 上传图片---SpringMVC学习笔记(十一)
  6. c# 字符串中多个连续空格转为一个空格
  7. VMware Converter P2V 时,卡住
  8. zookeeper集群,每个服务器上的数据是相同的,每一个服务器均可以对外提供读和写的服务,这点和redis是相同的,即对客户端来讲每个服务器都是平等的。...
  9. DAM的内涵正在改变
  10. 单行文本和多行文本溢出以省略号显示方法
  11. oracle+5秒钟一个间隔,ORACLE日期时间函数大全 (二)
  12. Tapestry 教程(七)在Tapestry中一起使用Hibernate
  13. PS 如何使用抽出滤镜抠人物的头发丝等细节
  14. 这可能是目前最好用的p2p外网访问内网软件(免费内网穿透)
  15. ERP实施-有色金属-铜冶炼
  16. 树莓派4B-安装64位操作系统
  17. Python生成对角矩阵和对角块矩阵
  18. 蓝牙音箱硬件设计分享
  19. 明日方舟 红松林故事集
  20. 云服务器+windows iis系统+微信App平台程序+vb.net后台服务开发流程

热门文章

  1. Percona Server
  2. android蓝牙设置特征属性,Android BLE蓝牙详细解读(二)
  3. mysql 5.6 登录 警告_解决mysql登录出现警告问题的简单方法
  4. linux 重定向_Linux视频教程分享,零基础在家你也可以学的会
  5. linux 脚本 格式化,Formatting Long Lines 格式化多行字符的shell脚本
  6. java基础的知识_Java基础知识点(一)
  7. JAVA打印变量类型
  8. Nginx 500错误总结
  9. 详细记录一次npm i canvas报错的解决过程
  10. nginx 编译出现的问题ngx_murmurhash.o failed