Computing average of time deltas

Discussion in 'Ruby' started by Peter Szinek, Mar 19, 2007.

  1. Peter Szinek

    Peter Szinek Guest

    Hello,

    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
    --------
    average 2s:000ms




    TIA,
    Peter
    __
    http://www.rubyrailways.com :: Ruby and Web2.0 blog
    http://scrubyt.org :: Ruby web scraping framework
    http://rubykitchensink.ca/ :: The indexed archive of all things Ruby
    Peter Szinek, Mar 19, 2007
    #1
    1. Advertising

  2. Peter Szinek

    Phrogz Guest

    On Mar 19, 5:58 am, Peter Szinek <> wrote:
    > 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
    > --------
    > average 2s:000ms


    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.
    Phrogz, Mar 19, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raider

    computing average value

    Raider, Feb 26, 2006, in forum: C++
    Replies:
    5
    Views:
    354
    Raider
    Feb 27, 2006
  2. about VHDL deltas

    , Dec 10, 2007, in forum: VHDL
    Replies:
    2
    Views:
    440
    Thomas Stanka
    Dec 11, 2007
  3. optical supercomputing

    Optical Computing: special issue - Natural Computing, Springer

    optical supercomputing, Dec 19, 2008, in forum: C Programming
    Replies:
    0
    Views:
    404
    optical supercomputing
    Dec 19, 2008
  4. optical supercomputing

    Optical Computing: special issue - Natural Computing, Springer

    optical supercomputing, Jan 16, 2009, in forum: C Programming
    Replies:
    0
    Views:
    438
    optical supercomputing
    Jan 16, 2009
  5. John Honovich

    Computing Deltas / Difflib for Ruby

    John Honovich, Mar 7, 2008, in forum: Ruby
    Replies:
    1
    Views:
    112
    Alex Fenton
    Mar 7, 2008
Loading...

Share This Page