Computing average of time deltas

P

Phrogz

Just to make sure I am not reinventing the wheel(tm) again: is there a
function/lib in Ruby which computes average of timespans? e.g.

first task took 1s:100ms
second task took 1s:400ms
third task took 3s:500ms

The Benchmark class lists an example of doing this for a fixed set of
discrete known tasks:

require 'benchmark'

n = 50000
Benchmark.benchmark(" "*7 + CAPTION, 7, FMTSTR, ">total:",
">avg:") do |x|
tf = x.report("for:") { for i in 1..n; a = "1"; end }
tt = x.report("times:") { n.times do ; a = "1"; end }
tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end }
[tf+tt+tu, (tf+tt+tu)/3]
end

The result:

user system total real
for: 1.016667 0.016667 1.033333 ( 0.485749)
times: 1.450000 0.016667 1.466667 ( 0.681367)
upto: 1.533333 0.000000 1.533333 ( 0.722166)
total: 4.000000 0.033333 4.033333 ( 1.889282)
avg: 1.333333 0.011111 1.344444 ( 0.629761)

It would also be pretty easy to modify the Benchmark.bmbm method to do
this. Just sum and average the results calculated on line 277:
res = Benchmark::measure(&item)


A side note: if you have a never-ending sequence of 'tasks' (such as a
task repeated every update frame in a renderer) it is easier to
calculate - and sometimes more desirable - to use a low-pass filter
rather than a running average on a circular- or windowed-list.

For example:
avg_task_time += ( current_task_time - avg_task_time ) / inertia

The higher the 'inertia', the less the formula will react to
measurement spikes and the longer it will take to reach a new average.
The closer 'inertia' is to zero, the quicker it will react to changes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top