如果是两种情况,哪个更快? [英] Which is faster of two case or if?

查看:64
本文介绍了如果是两种情况,哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须制作一个非常"轻便的脚本,该脚本将吸收用户的选项并调用脚本中的函数来执行某些任务.

I have to make a "very" light script which would take in options from user and call in functions within the script to perform some tasks.

现在我可以同时使用IF和CASE选项,但是我想知道两者之间哪一个更轻.当我将CASE放到脚本中时,CASE显然看起来不太复杂,它在计算/CPU使用率/内存使用方面还是使我的脚本更轻巧,或者说IF更好?

Now I can us both IF and CASE options, but I want to know which among both will be lighter. CASE obviously looks less complex when I put it in script, does it make my script lighter as well in terms of computation/CPU usage/memory usage or IF is better ?

该脚本将有大约10-15个输入选项供您选择,即否".如果增加了新功能,将来选择的选项可能会增加.

The script will have around 10-15 input options to select from, the no. of option for choosing might increase in future, if new functionality gets added.

注意:-适用于在Solaris 10/RHEL 6上运行的bash中

Note:- It's for usage in bash running on Solaris 10/RHEL 6

推荐答案

尽管我同意bash本身很慢的其他评论,但我还是在这里执行了一些测试来检查差异.平台是在慢速计算机上的Ubuntu 10.10.没有其他进程并行运行.

Although I agree with other comments that bash is itself slow, I just executed some tests here to check the difference. Platform is Ubuntu 10.10 on a slow machine. No other processes running in parallel.

CASE花费的时间不到一半,这令人惊讶:

CASE is taking less than half the time, which is quite surprising:

user@machine:~$ time for i in {1..1000000}; do if [ "$i" == "45"  ]; then echo $i; elif [ "$i" == "50" ]; then echo $i; fi; done
45
50

real    0m22.154s
user    0m21.750s
sys     0m0.380s

user@machine:~$ time for i in {1..1000000}; do case "$i" in "45") echo $i;; "50") echo $i;; esac; done
45
50

real    0m10.286s
user    0m10.230s
sys     0m0.040s

重复实验,但添加第三次比较:

Repeating the experiment, but adding a third comparison:

user@machine:~$ time for i in {1..1000000}; do if [ "$i" == "45"  ]; then echo $i; elif [ "$i" == "50" ]; then echo $i; elif [ "$i" == "6000" ]; then echo $i; fi; done
45
50
6000

real    0m32.602s
user    0m32.070s
sys     0m0.510s

user@machine:~$ time for i in {1..1000000}; do case "$i" in "45") echo $i;; "50") echo $i;; "6000") echo $i;; esac; done
45
50
6000

real    0m13.553s
user    0m13.520s
sys     0m0.010s

看起来IF只是简单地重复了3次比较,而CASE进行了一次比较,这可以解释为什么CASE几乎是恒定的,而IF似乎花费的时间与比较次数成正比.

It looks like IF simply repeats a comparison 3 times while CASE makes a single comparison, which could explain why CASE is almost constant while IF seems to take a time that is proportional to the number of comparisons.

现在检查建议的[[$ i == 45]]:

Now checking the suggested [[ $i == 45 ]]:

user@machine:~$ time for i in {1..1000000}; do if [[ $i == 45  ]]; then echo $i; elif [[ $i == 50 ]]; then echo $i; elif [[ $i == 6000 ]]; then echo $i; fi; done
45
50
6000

real    0m15.127s
user    0m15.090s
sys     0m0.010s

user@machine:~$ time for i in {1..1000000}; do case $i in 45) echo $i;; 50) echo $i;; 6000) echo $i;; esac; done
45
50
6000

real    0m9.966s
user    0m9.940s
sys     0m0.010s

同样,CASE更快,但没有那么快.

Again, CASE is faster, but not that faster.

要尝试确定浪费在FOR-LOOP本身上的时间,让我们尝试几乎不运行任何内容:

To try to determine the time wasted on the FOR-LOOP itself, let's try to run almost nothing:

user@machine:~$ time for i in {1..1000000}; do x=0; done

real    0m5.095s
user    0m5.070s
sys     0m0.010s

这篇关于如果是两种情况,哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆