[项目实践] 在项目实战中提升代码效率的的一次应用实践-----使用列表解析式输出当前android设备的CPU核数

1、先按照初学者思维代码应该是这样的

 1 #coding:utf-8
 2 import os 
 3 text = os.popen("adb shell command cat /proc/cpuinfo",'r').read().split("\n")
 4 i = 0
 5 for j in text:
 6     if "processor\t:" in j:
 7         i += 1
 8 if i == 1:
 9     print ("单核")
10 elif i == 2:
11     print ("2核")
12 elif i == 3:
13     print ("3核")
14 elif i == 4:
15     print ("4核")
16 elif i == 5:
17     print ("5核")
18 elif i == 6:
19     print ("6核")
20 elif i == 7:
21     print ("7核")
22 elif i == 8:
23     print ("8核")

上面的代码也没啥毛病,也能得出正确的结果,但代码有20多行,实在是影响代码的执行效率了

 

2、接下来看这个

1 #coding:utf-8
2 import os 
3 i = 0
4 cores = ["单核","2核","3核","4核","5核","6核","7核","8核"]
5 text = os.popen("adb shell command cat /proc/cpuinfo",'r').read().split("\n")
6 lst = [i+1 for j in text if "Processor\t:" in j]
7 cpu_cores = cores[len(lst)-1]
8 print (cpu_cores)

得出的结果跟上面的一样,那这样看看哪个的执行效率更好些

posted @ 2018-11-07 13:37  iSZ  阅读(192)  评论(0编辑  收藏  举报