DigitalLibrarian / VS2010Projects

Bunch of unorganized mess
1 stars 1 forks source link

float Smoother #38

Open DigitalLibrarian opened 8 years ago

DigitalLibrarian commented 8 years ago

We need a generic smoothing class that operates on float. It exposes a value that is the running average for the last N calls for some observed value V. This can be used for many things.

The chief reason for it, atm, is to have smoothed fps displays, showing the average for the last 60 frames, for example. We currently display the instantaneous fps, which isn't stable or useful as a load metric.

DigitalLibrarian commented 8 years ago

This should work

public class Smoother { public float Value { get { return ComputeLatest(); }}

int MaxSamples;
List<float> Samples { get; set; }
public Smoother(int maxSamples)
{
    MaxSamples = maxSamples;
    Samples = new List<float>();
    CurrentSample = 0;
}

public void Update(float v)
{
    if(CurrentSample == Samples.Count){
        Samples.Add(v);
    }else{
        Samples[CurrentSample] = v;
    }
    IncSample();
}

int CurrentSample;
void IncSample()
{
    CurrentSample++;
    CurrentSample %= MaxSamples;
}

float ComputeLatest()
{
    int numSamples = Samples.Count();
    if (numSamples == 0) return 0f;
    float total = 0f;
    foreach(var sample in Samples)
    {
        total += sample;
    }
    return total / (float) numSamples;
}

}

DigitalLibrarian commented 8 years ago

http://stackoverflow.com/questions/8779936/what-is-the-correct-way-to-calculate-the-fps-given-that-gpus-have-a-task-queue-a