# Looking for optimization help.

I would like to know if this is about as optimized it can get, i’m not worried about too much accuracy with the timer and I might make a custom round function if that would help.

``````    [Range(0.01f, 10)]

private Text fpsDisplay;
private float nextUpdate;

private void Start()
{
fpsDisplay = GetComponent<Text>();
}

private void Update()
{
nextUpdate += Time.unscaledDeltaTime;
{
nextUpdate = 0;
float i = 1f / Time.unscaledDeltaTime;
fpsDisplay.text = "FPS:" + Mathf.Round(i * 10f) / 10f + "(" + Mathf.Round((1000f / i) * 10f) / 10f + "ms)";
}
}
``````

Basic run-down: we calculate the fps and mspf and run it every set period of time i’m mainly worried about the rounding and all the adding of strings I did.

If trying to get frames per second, the most optimized way I know is :

``````int m_frameCounter = 0;
float m_timeCounter = 0.0f;
public static float fps = 0.0f; // hidden in inspector, but called easy (ClassName.fps)
float m_refreshTime = 0.5f; // fraction of second to check (0.5 = half)

void GlobalFramesPerSecond()
{
if (m_timeCounter < m_refreshTime)
{
m_timeCounter += Time.deltaTime;
m_frameCounter++;
}
else
{
fps = (float)m_frameCounter / m_timeCounter;
m_frameCounter = 0;
m_timeCounter = 0.0f;
}
}
``````

But true, any use of strings(especially checking against) is pretty performance heavy. But as they say, “You gotta do what you gotta do!”, lol…

However I could interest you into learning about Interpolated strings, changing to this:

``````fpsDisplay.text =
\$"FPS: {Mathf.Round(i * 10f) / 10f}
( {Mathf.Round((1000f / i) * 10f) / 10f} ms)";
``````

^ Separated only for easy use of reading. But the `\$` before the string, makes the value interpolated, and much easier to write and read. Any values or variables incapsulated with `{ }` .

But, at first glance, your math seems solid, so I don’t think there’s a better way to perform it. Some would argue overall readability, making it :

``````nextUpdate += Time.unscaledDeltaTime;