How to multiply a benchmark

2019-09-20 10:55发布

问题:

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.

回答1:

The problem with extrapolating, as you suggest, is that it compounds any errors you've got. Running N rounds of a single benchmark tends to even out any irregularities. Running 1 round and multiplying by N actually amplifies any irregularities.

So the short answer is you can't. You need to run multiple tests to get a better idea of performance. If these tests take a long time to run you either have to eat the cost or find ways of running them more quickly, or in parallel somehow.



回答2:

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.