Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove delegate allocation in jitter calculation in FramedClock #6122

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions osu.Framework/Timing/FramedClock.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Extensions.TypeExtensions;
using System;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Extensions.TypeExtensions;

namespace osu.Framework.Timing
{
Expand Down Expand Up @@ -85,9 +84,18 @@ public virtual void ProcessFrame()
FramesPerSecond = (int)Math.Ceiling(framesSinceLastCalculation * 1000f / timeSinceLastCalculation);

// simple stddev
double avg = betweenFrameTimes.Average();
double stddev = Math.Sqrt(betweenFrameTimes.Average(v => Math.Pow(v - avg, 2)));
Jitter = stddev;
double sum = 0;
double sumOfSquares = 0;

foreach (double v in betweenFrameTimes)
{
sum += v;
sumOfSquares += v * v;
}

double avg = sum / betweenFrameTimes.Length;
double variance = (sumOfSquares / betweenFrameTimes.Length) - (avg * avg);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am shook that this can be done and be actually correct.

Proof:

$$ \begin{align} \sigma_x &= \frac{\sum_i (x_i - \bar{x})^2}{|x|} = \\ &= \frac{\sum_i (x_i^2 - 2x_i \bar{x} + \bar{x}^2)}{|x|} = \\ &= \frac{\sum_i x_i^2}{|x|} - 2 \bar{x} \frac{\sum_i x_i}{|x|} + \bar{x}^2 \cdot \frac{\sum_i 1}{|x|} = \\ &= \frac{\sum_i x_i^2}{|x|} - 2 \bar{x} \cdot \bar{x} + \bar{x}^2 \cdot \frac{|x|}{|x|} = \\ &= \frac{\sum_i x_i^2}{|x|} - \bar{x}^2 \end{align} $$

wherein $x_i$ are elements of a set, $\bar{x}$ is the mean of the set, and $|x|$ is the cardinality of the set.

I thought there was no way this was gonna work but kept failing to find counterexamples...

Jitter = Math.Sqrt(variance);
}

timeSinceLastCalculation = framesSinceLastCalculation = 0;
Expand Down
Loading