Help us understand the problem. What is going on with this article?

文字列置換方法の違いによるスピードの比較

More than 1 year has passed since last update.

文字列置換方法の違いによるスピードの比較

by shimizu14
1 / 2
  • 文字列置換の方法によってどれくらいスピードに違いがあるのか確認してみました。
  • 今回のサンプルは単純な文字列置換ですが、結果はsedコマンドがawkコマンドの70%ほどの時間で完了することがわかりました。
  • bashのwhileの中で置換する方法も2つ確認しましたが、bash変数の文字列置換とsedコマンドを使う方法では750倍以上の差が出ました。

ソースコード

q_01_gorilla.sh
#!/bin/bash

FileFrom="Gorilla.tmp"
FileTo="Gallus.tmp"

StringFrom="Gorilla gorilla"
# StringTo "Gallus gallus"

# 100万行のファイルを準備します
rm -f "${FileFrom}"
for (( i = 0; i < 1000000; i++ )); do
  echo "${StringFrom}" >> "${FileFrom}"
done

# sedコマンドで置換します
echo -e "\n### sed ###"
rm -f "${FileTo}"
cat "${FileFrom}" > /dev/null
time sed 's/orilla/allus/g' "${FileFrom}" > "${FileTo}"
cat -n "${FileTo}" | tail -n 2

# awkコマンドで置換します
echo -e "\n### awk ###"
rm -f "${FileTo}"
cat "${FileFrom}" > /dev/null
time awk '{gsub("orilla","allus",$0) ; print $0}' "${FileFrom}" > "${FileTo}"
cat -n "${FileTo}" | tail -n 2

# whileの中でbashの変数置換機能を使用します
echo -e "\n### bash ###"
rm -f "${FileTo}"
cat "${FileFrom}" > /dev/null
time cat "${FileFrom}" | while read line; do
  StringTo="${line//orilla/allus}"
  echo "${StringTo}"
done > "${FileTo}"
cat -n "${FileTo}" | tail -n 2

# whileの中でsedコマンドで置換します
echo -e "\n### sed-2 ###"
rm -f "${FileTo}"
cat "${FileFrom}" > /dev/null
time cat "${FileFrom}" | while read line; do
  echo "${StringFrom}" | sed 's/orilla/allus/g'
done > "${FileTo}"
cat -n "${FileTo}" | tail -n 2

実行結果

$ ./q_01_gorilla.sh

### sed ###

real    0m0.121s
user    0m0.092s
sys 0m0.012s
 99999  Gallus gallus
100000  Gallus gallus

### awk ###

real    0m0.171s
user    0m0.144s
sys 0m0.004s
 99999  Gallus gallus
100000  Gallus gallus

### bash ###

real    0m2.861s
user    0m2.012s
sys 0m0.724s
 99999  Gallus gallus
100000  Gallus gallus

### sed-2 ###

real    3m4.987s
user    0m6.748s
sys 0m25.956s
 99999  Gallus gallus
100000  Gallus gallus

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away