Search code examples
rubybenchmarking

How to multiply a benchmark


Say I have a benchmark result like so:

0.020000   0.000000   0.020000 (  0.020197)

I'm creating this with something like

Benchmark.bm do |x|
  x.report { run_a_method() }
end

This represents the time required to call a method foo one time.

I want to produce a benchmark which shows the result of running foo 3 times, but only requires calling the method once.

This can be done by simply multiplying the values in the first benchmark by 3.

Is there any way to do this?

Edit

I'm not really appreciating the comments that this is "not a correct benchmark". I recognize that it is more accurate to run it multiple times, but these are resource-intensive tests and I am simply projecting an expected time frame for multiple runs.

In other words, my use-case is just to create an expectation for the multiple-run time, not a wholly accurate one.


Solution

  • Figured it out myself.

    Initially I tried the following:

    report = Benchmark.bm do |x|  
      x.report { sleep.0.5 }
    end
    # => report is an array with     
    #    1 element
    
    report * 2
    # => shows an array with 2 elements
    #    (the first is duplicated)
    

    But I eventually thought to try

    report[0] * 2
    

    which had the intended effect of doubling the values in the benchmark.

    For example, if the report variable points to

    [#<Benchmark::Tms:0x0055fcba149030
    @label="", 
    @real=0.5001437529999748, 
    @cstime=0.0, @cutime=0.0, 
    @stime=0.0, @utime=0.0, @total=0.0>]
    

    The running puts (report[0] * 2) shows

      0.000000   0.000000   
    0.000000 (  1.000288)
    => nil
    

    which is the multiplied value I was going for.