标签:click time read test success for 技术 echo shell
1 #!/bin/bash 2 start_time=`date "+%s"` 3 echo "start_time is $start_time" 4 for((i=1;i<=4;i++)) 5 do 6 echo success $i;sleep 2 7 done 8 wait 9 end_time=`date "+%s"` 10 echo "end_time is $end_time" 11 echo "TIME: `expr $end_time - $start_time`"
上面的wait是多余的;执行的结果如下:
1 start_time is 1584504244 2 success 1 3 success 2 4 success 3 5 success 4 6 end_time is 1584504252 7 TIME: 8
由于是顺序执行,每次只执行一个,所以使用8s完成
1 #!/bin/bash 2 start_time=`date "+%s"` 3 echo "start_time is $start_time" 4 for((i=1;i<=4;i++)) 5 do 6 { 7 echo success $i;sleep 2 8 }& 9 done 10 wait 11 end_time=`date "+%s"` 12 echo "end_time is $end_time" 13 echo "TIME: `expr $end_time - $start_time`"
执行的结果如下:
1 start_time is 1584504404 2 success 1 3 success 2 4 success 3 5 success 4 6 end_time is 1584504406 7 TIME: 2
由于是并行执行,所以使用2s完成;加入各个进程执行时间
1 #!/bin/bash 2 3 trap "exec 1000>&-;exec 1000<&-;exit 0" 2 4 5 mkfifo testfifo 6 exec 1000<>testfifo 7 rm -rf testfifo 8 9 for((n=1;n<=10;n++)) 10 do 11 echo >&1000 12 done 13 14 start=`date "+%s"` 15 16 for((i=1;i<100;i++)) 17 do 18 read -u1000 19 { 20 echo success $i; sleep 2 21 echo >&1000 22 }& 23 done 24 25 wait 26 end=`date "+%s"` 27 28 echo "TIME: `expr $end - $start`" 29 30 exec 1000>&- 31 exec 1000<&-
执行结果如下:
1 success 1 2 success 2 3 success 3 4 success 4 5 success 5 6 success 6 7 success 7 8 success 8 9 success 9 10 success 10 11 success 11 12 success 12 13 success 13 14 success 14 15 success 15 16 success 16 17 success 17 18 success 18 19 success 19 20 success 20 21 success 21 22 success 22 23 success 23 24 success 24 25 success 25 26 success 26 27 success 27 28 success 28 29 success 29 30 success 30 31 success 31 32 success 32 33 success 33 34 success 34 35 success 35 36 success 36 37 success 37 38 success 38 39 success 39 40 success 40 41 success 41 42 success 42 43 success 43 44 success 44 45 success 45 46 success 46 47 success 47 48 success 48 49 success 49 50 success 50 51 success 51 52 success 52 53 success 53 54 success 54 55 success 55 56 success 56 57 success 57 58 success 58 59 success 59 60 success 60 61 success 61 62 success 62 63 success 63 64 success 64 65 success 65 66 success 66 67 success 67 68 success 68 69 success 69 70 success 70 71 success 71 72 success 72 73 success 73 74 success 74 75 success 75 76 success 76 77 success 77 78 success 78 79 success 79 80 success 80 81 success 81 82 success 82 83 success 83 84 success 84 85 success 85 86 success 86 87 success 87 88 success 88 89 success 89 90 success 90 91 success 91 92 success 92 93 success 93 94 success 94 95 success 95 96 success 96 97 success 97 98 success 98 99 success 99 100 TIME: 20
上述的example每次执行10个线程,每次需要2秒执行完成,总共执行100个线程,总计20秒。
标签:click time read test success for 技术 echo shell
原文地址:https://www.cnblogs.com/zhiminyu/p/12516600.html