LoginSignup
0
0

【Ruby】処理の実行時間を知りたい

Last updated at Posted at 2023-12-13

背景

・leetCodeをやっていると処理時間が表示されるので、パフォーマンス改善を意識することができる。
・ただし、submitした状況によって処理時間にブレが発生する。
・難易度が簡単なやつほど、処理が単純なのでほどブレが大きい。
→ ローカルでやってみるか。

実装(サンプル)

sample.rb
require 'active_support'
require 'active_support/core_ext/benchmark'
result = Benchmark.ms do
  # 処理
end
puts "処理概要 #{result.ceil(5)}s"

解説

・Rubyには標準でBenchmarkというモジュールがある。
・msメソッド(ミリ秒で表示する機能)を使うには、active_supportでbenchmark.rbが拡張されているものを使う必要がある。
・小数点以下がすごく長いので、適当な位置でceilメソッドを使用してみた。

参考

0
0
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
0
0