Let and expr shell operations compare let's win

Source: Internet
Author: User

Integer operations in shell scripts are generally implemented by the two instructions of let and expr, such as the variable s plus 1 can be written: let "s = $s + 1" or s= ' expr $s + 1 '
The two are not easy to write, but the performance of the comparison can be an example to show you:

1. Expr script and execution time:

  

#!/bin/bashs=0while [$s-lt 10000]do       s= ' expr $s + 1 '       echo $s >>/dev/null 2>&1done[[email protected] ~]# time sh expr.sh real    0m50.776suser    0m11.528ssys     0m39.601s

2. Let script and execution time:

#!/bin/bashs=0while [$s-lt 10000]do let       "s = $s + 1"       echo $s >>/dev/null 2>&1done[[email protected ] ~]# time sh let.sh real    0m0.875suser    0m0.676ssys     0m0.199s

With the above two examples, it goes without saying that it is clear that let's win with super-high advantage, so get expr dozens of times times!

Transferred from: http://yooma.blog.51cto.com/6965726/1187063/

Let and expr shell operations compare let's win

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.