4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ruby でブロックの実行時間を計る

4
Posted at

たまに Time.now - start 的なことを書くので、記録。

def measure(&block)
  t0 = Time.now
  ret = block.call
  span = Time.now - t0
  return ret, span
end

利用方法は以下のような。

ret, time = measure do
  sleep 1
  "hello"
end

puts "return = #{ret}, time = #{time}" 
  => "return = hello, time = 1.00064"

実は変数の同時代入構文が好きです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?