Friday, May 25, 2007

Ruby function timer

For quick performance tests, I often need to time the execution of some function. Here is a short function to do it in Ruby:

def time_func(&block)
start_time = Time.now
yield
end_time = Time.now
return end_time - start_time
end

Usage is straightforward:

time_func do my_test_function(args...) end

No comments: