Performance Benefits of Using Public Static Class for Global Vars

Hi.

This is my first post in the Unity forums. I’ve been C# coding in Unity since January 2017 and just love it. The online tutorials are great. A shout out of thanks to all those who have created great tutorials on YT including Sebastian Lague, Brackeys, and Hibby to name a few!

Have been building a cybersecurity related application and in the beginning I shared variables (ints, bools, floats, arrays of the same) between objects and C# scripts by the standard GameObject.Find().GetComponent<>() (and similar) methods.

However, recently I was in code housekeeping mode recently (after watching many Sebastian Lague videos) and I rewrote most of my code to use a public static classes for “Globals” and for methods which do not inherit from MonoBehavior. This was not specifically recommended by Sebastian (or anyone); but it just seemed easier to manage and cleaner (for me, at least).

Sorry if this is a dumb post, as I just transitioned from programming mostly in PHP to a “hopeful” C# programmer around 8 months ago; and am enjoying learning C# and very passionate about Unity and the community at large.

To the point, I noticed a huge increase in FPS rate when I moved to public static classes that do not inherit from MonoBehavior for all these shared variables between scripts and objects**.**

Is this normal? If so, why or why not?

Why do I feel like this is a dumb question? LOL

Thanks!

In order to understand what’s going on, you would have profile both cases and compare the results. This will tell you exactly how and where performance changed. If you’re new to the Unity Profiler, the following videos might be helpful.

Introduction to the Profiler

Unite Europe 2017 - Performance optimization for beginners

Once you’re a little more familiar with profiling, you’ll most likely notice that FPS is not a useful measurement for performance. If you’re interested why, you might want to read this page:
https://www.mvps.org/directx/articles/fps_versus_frame_time.htm

1 Like

Hi Peter,

Thanks for replying.

Actually, I recently started using the profiler (about a month ago, after I changed the code to use more public static classes per my original post) and quickly found a few issues in Update() in a few GOs; and because of the excellent design of the profiler (using deep profiling) was quickly able to make some changes which increased performance even more. The Unity profiler is really great. I think it was back then when I finally became a Unity Plus Person (a UPPy, LOL).

Regarding, FPS, yeah, like the “always debated three load average numbers” on a unix / linux system, “load averages” or “fps” are not perfect for sure but I’m OK with them in general ; and they work “good enough” for me in most normal situations when looking for clues before diving down.

However, kinda to your point indirectly, I was on a linux web server the other day and the performance was terrible and the load averages were fine; turned out after trouble shooting that a single database table created a huge amount of overhead and this was slowing down the response times more than the cpu load; and just doing a simple optimize of the table solved that problem. Yet, even thought the flawed load avg numbers did not provide much direct help; they made me pause to look elsewhere, in this case at the DB performance. Saved the need to reboot on a busy web server during peak hours.

Likewise, though FPS is not perfect and is flawed, it does provide a lot of good clues to me; and it’s easy to see and also feel when the FPS is very low, the app is performing poorly in general, and when FPS is high, it’s kicking ass, generally, LOL; and the more I optimize (null out large arrays that are not in use, do periodic garbage collection, clean up Update() code, etc, I generally see FPS improve as well… so it’s and OK enough indicator in a number of simple checks along the way.

I’m not religious about code, and seem to paint / code in the abstract like an impressionist when writing code; then cleaning up my mess later, versus being a great coder who can code well from the ground up (like all the people I admire). So, after posting I noticed some religious debates about Globals, etc. so I hope this thread does not pivot to code religion. What’s “best” is what works for the coder… and what inspires them to code and create, and what works for one person often is not motivational for another.

Anyway, in my case, when I moved all these GO references between scripts to a public static class which did not inherit from Mono, my “user tester” was like, WOW, it’s really faster now!!

Unity Rocks!