为什么Bash脚本循环很慢?

时间:2018-11-29 10:49:22

标签: linux bash shell

我想使用bash脚本来计算一些数字:

countnum=0

while [ $countnum -lt 200000 ];do
    countnum=$(($countnum + 1))
    # something else
done

然后我得到了:

$ time sh c.sh

real    0m3.981s
user    0m3.642s
sys 0m0.303s

任何I / O持续约4秒? 为什么?

以下是用Java程序编写的周期2000w数据的时间源和输出。花费了53毫秒

public class AG {
    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        int waitSplitNum = 20000000;
        int wegiht = 4;

        int cyclesize = waitSplitNum / wegiht;
        int remainder = waitSplitNum % wegiht;
        if (remainder > 0)
            cyclesize++;

        int tmpnum = 0;
        int countnum = 0;

        while (countnum < waitSplitNum) {
            tmpnum++;

            countnum++;

            if (tmpnum == cyclesize) {
                System.out.println((countnum - tmpnum + 1) + " " +     countnum);
                tmpnum = 0;
            }
        }
        if (tmpnum != 0)
            System.out.println((countnum - tmpnum + 1) + " " +     waitSplitNum);

        long end = System.currentTimeMillis();
        System.out.println("spend time: " + (end - start) + " ms.");

    }
}

输出为:

1 5000000
5000001 10000000
10000001 15000000
15000001 20000000
spend time: 53 ms.

0 个答案:

没有答案