LoginSignup
23
23

More than 5 years have passed since last update.

Rubyでプログラムの処理速度を計測する

Last updated at Posted at 2015-04-26

Rubyでは、言語に付属しているbenchmarkライブラリを使って簡単に速度計測が出来ます。

使い方
require 'benchmark'

# ラベル行の表示
puts Benchmark::CAPTION

# データ計測
result = Benchmark.measure{
  # 計測したい処理
  require 'xmlsimple'
  hash = XmlSimple.xml_in(open('path/to/file.xml'));
}

# 結果表示
puts result
実行結果
      user     system      total        real
 11.160000   0.160000  11.320000 ( 11.402827)

なお、計測結果の読み方は以下の通りです(基本、Linuxのtimeコマンドと一緒です)。

  • user
    • ユーザモード(ユーザプログラムを実行するモード)で消費するCPU時間
  • system
    • システムモード(OSのプログラムを実行するモード)で消費するCPU時間
  • total
    • userとsystemの合計
  • real
    • 実行(呼び出しから終了まで)にかかった実時間
23
23
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
23
23