Skip to content

Instantly share code, notes, and snippets.

@lucasteles
Created April 3, 2025 02:22
Show Gist options
  • Save lucasteles/84739da2b3ff6d102cad3ef5b53e555a to your computer and use it in GitHub Desktop.
Save lucasteles/84739da2b3ff6d102cad3ef5b53e555a to your computer and use it in GitHub Desktop.
FPS Counter
using System.Diagnostics;
public sealed class FpsCounter
{
double currentFps;
long lastFrameTime;
const float SmoothFactor = 0.1f;
public FpsCounter() => Reset();
public double Current => Math.Round(currentFps, 2);
public void Update()
{
var delta = Stopwatch.GetElapsedTime(lastFrameTime);
Update(delta.TotalSeconds);
lastFrameTime = Stopwatch.GetTimestamp();
}
void Update(double deltaTime)
{
var newFps = 1 / deltaTime;
currentFps = ((1 - SmoothFactor) * currentFps) + (SmoothFactor * newFps);
}
public void Reset()
{
currentFps = FrameRate.ExpectedFps;
lastFrameTime = Stopwatch.GetTimestamp();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment