LoginSignup
8
3

More than 5 years have passed since last update.

コマンドの実際の実行時間を `time` コマンドを使わずに計測する

Last updated at Posted at 2018-03-05

背景

  • 既存のShell Scriptをちょっと改変して実際の実行時間を出力したい。
  • 後でちょっと集計したいから標準出力に時間だけを出力したい。
  • ミリ秒くらいの粒度で測定したい。

実装

start_time=$(date +"%s.%3N")
$command > /dev/null
end_time=$(date +"%s.%3N")
echo "scale=1; $end_time - $start_time" | bc

%3N はミリ秒の上位3桁をとる date のフォーマット指定子です。BSD版ではこの指定子は使えないので、Macの場合はHomebrewで coreutils をインストールして gdate コマンドを使います。

所感

date コマンドでミリ秒を指定するフォーマットや、BASHで小数を計算する方法など、まだまだ知らないことばかりです。

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3