Thursday, December 27, 2007

Tiny profiling utility


def prof(t = 1)
  starting_time = Time.now
  t.times { yield }
  dlta = (Time.now - starting_time).to_f
  puts "time: #{dlta.to_s}(s) . req/s: #{(1/dlta)}"
end

  • Use prof to measure how long a function or any block of code takes.
  • Use parameter t>1 to run the block code t times and return the average time per test
Examples:

>> prof { Flight.find_first }
time: 0.002032(s) . req/s: 492.125984251969
=> nil
>> prof { Flight.find_first }
time: 0.00201(s) . req/s: 497.512437810945
=> nil
>> prof(10) { Flight.find_first }
time: 0.001351(s) . req/s: 740.19245003701
=> nil
>> prof(100) { Flight.find_first }
time: 0.0012727(s) . req/s: 785.731122809775
=> nil
>> prof(1000) { Flight.find_first }
time: 0.001415021(s) . req/s: 706.703292742652
=> nil
>> prof(10000) { Flight.find_first }
time: 0.0013922995(s) . req/s: 718.236270285237
=> nil
>>

0 comments:

Post a Comment