Last active
July 1, 2016 13:29
-
-
Save lamsh/e9945fe252cc183f0c46c0e2c9515d7e to your computer and use it in GitHub Desktop.
シェルスクリプトでの列フィールド取得方法の実行速度の比較(http://myfuturesightforpast.blogspot.jp/2016/07/how-to-extract-column-field-in-shell.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# \file loop-time.sh | |
# \author SENOO, Ken | |
# \copyright CC0 | |
set -u | |
DATA="loop-data.dat" | |
: > $DATA | |
for i in $(seq 1 50000) | |
do | |
echo "1,2,3,4,5,6,7,8,9,0" >> $DATA | |
done | |
timeit_loop(){ | |
start=$(date +%s) | |
cat $DATA | | |
( | |
while read -r line | |
do | |
last_field="$(eval $1)" | |
done | |
end=$(date +%s) | |
dt=$((end - start)) | |
echo "time: $dt [s], last field: ${last_field}, $1" | |
) | |
} | |
LASTCOL=$(($(head -n 1 $DATA | grep -o ',' | wc -l) + 1)) | |
timeit_loop 'echo "$line" | rev | cut -d ',' -f 1 | rev' | |
timeit_loop 'echo "$line" | cut -d ',' -f $LASTCOL' | |
timeit_loop 'echo "$line" | awk -F "," "{print \$NF}"' | |
timeit_loop 'echo "$line" | grep -o [^,]*$' | |
timeit_loop 'echo "$line" | sed "s/^.*,\([^,]*\)$/\1/"' | |
timeit_loop 'echo ${line##*,}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment