码迷,mamicode.com
首页 > 其他好文 > 详细

for 和while 对cpu和内存的影响

时间:2017-07-04 14:58:36      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:系统性能

关于while for循环的cpu性能以及内存占用情况

 同样的脚本文件,分别使用for和while完成,再用top查看cpu和内存的情况。


[root@localhostfish]# cat sumwhile.sh

#!/bin/bash

#

declare-i sum=0

declare-i i=1

 

while [ $i -le 1000000 ];do

letsum+=$i

let i++

done

 

echo $sum

[root@localhostfish]# top -b -d 0.1 >>sum_while_top

[root@localhostfish]# grep 40392 sum_while_top

4039240279 root      20   0 103m 1196 1040 R 95.5  0.1   0:16.36 sumwhile.sh   内存占用情况以及占用cpu的时间

4039240279 root      20   0 103m 1196 1040 R 94.9  0.1   0:16.46 sumwhile.sh

4039240279 root      20   0 103m 1196 1040 R 100.0  0.1   0:16.58 sumwhile.sh

4039240279 root      20   0 103m 1196 1040 R 94.7  0.1   0:16.68 sumwhile.sh

0:00.64sumwhile.sh

 

[root@localhostfish]# cat sumfor.sh

#!/bin/bash

#

declare-i sum=0

 

for i in `seq 1 1000000`; do

letsum+=$i

done

 

echo $sum

 

top -b -d0.1 >> sum_for_top

 

 

 

[root@localhostfish]# grep 40485 sum_for_top

4048540279 root      20   0 201m  99m 1048 R 100.0 10.0   0:43.45 sumfor.sh

4048540279 root      20   0 201m  99m 1048 R 94.7 10.0   0:43.55 sumfor.sh

4048540279 root      20   0 201m  99m 1048 R 95.0 10.0   0:43.65 sumfor.sh

4048540279 root      20   0 201m  99m 1048 R 100.0 10.0   0:43.76 sumfor.sh

4048540279 root      20   0 201m  99m 1048 R 100.0 10.0   0:43.87 sumfor.sh

  for循环对内存的占用和对cpu的占用都明显高于while循环。 


求解答原因。


for 和while 对cpu和内存的影响

标签:系统性能

原文地址:http://12833256.blog.51cto.com/12823256/1944382

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!