blog.euxn.me

shell のチルダと$HOMEのどちらが解決が早いか雑に試したら$HOMEの方が早かった

2017-11-19 Sun.

以下検証結果。文字列に変更が加わっているので参照渡しではないはず……。

tilda.sh
1for i in $(seq 1 1000000); do
2 str=~/tmp
3done
4echo $str
1~/s/g/e/s/shell ❯❯❯ time sh tilda.sh
2/Users/yuta/tmp
3sh tilda.sh 4.28s user 0.06s system 100% cpu 4.305 total
4~/s/g/e/s/shell ❯❯❯ time sh tilda.sh
5/Users/yuta/tmp
6sh tilda.sh 4.29s user 0.06s system 100% cpu 4.318 total
7~/s/g/e/s/shell ❯❯❯ time sh tilda.sh
8/Users/yuta/tmp
9sh tilda.sh 4.37s user 0.06s system 100% cpu 4.397 total
10~/s/g/e/s/shell ❯❯❯ time sh tilda.sh
11/Users/yuta/tmp
12sh tilda.sh 4.30s user 0.06s system 100% cpu 4.322 total
13~/s/g/e/s/shell ❯❯❯ time sh tilda.sh
14/Users/yuta/tmp
15sh tilda.sh 4.68s user 0.10s system 99% cpu 4.812 total
home.sh
1for i in $(seq 1 1000000); do
2 str=$HOME/tmp
3done
4echo $str
1~/s/g/e/s/shell ❯❯❯ time sh home.sh
2/Users/yuta/tmp
3sh home.sh 4.23s user 0.06s system 100% cpu 4.252 total
4~/s/g/e/s/shell ❯❯❯ time sh home.sh
5/Users/yuta/tmp
6sh home.sh 4.19s user 0.06s system 100% cpu 4.207 total
7~/s/g/e/s/shell ❯❯❯ time sh home.sh
8/Users/yuta/tmp
9sh home.sh 4.14s user 0.06s system 101% cpu 4.157 total
10~/s/g/e/s/shell ❯❯❯ time sh home.sh
11/Users/yuta/tmp
12sh home.sh 4.28s user 0.06s system 101% cpu 4.303 total
13~/s/g/e/s/shell ❯❯❯ time sh home.sh
14/Users/yuta/tmp
15sh home.sh 4.27s user 0.06s system 100% cpu 4.299 total