In Ruby or Rails, is there a "duration_for { fetch_some_data() }" and it can print "fetch_some_data() done, took 2.1 seconds"?
In Rails, is there already such a function?
So that we can use
<%= duration_for { fetch_some_data() } %>
and it will print
fetch_some_data() done, result is:
{"status" : 1, "data" : {"x" : 123} }
(took 2.1 seconds)
Otherwise, what is a good way to write tha开发者_JS百科t in Ruby? (more like a method, so we can't use the UNIX's time
to do it)
require 'benchmark'
Benchmark.ms do
fetch_some_data()
end
will return the time it takes to complete the block in milliseconds.
http://apidock.com/ruby/Benchmark
精彩评论