C# basic perfomance question


since there is a lot of different ways to achieve any result I cannot keep asking myself what is more optimizable and better in perfomance view and keep overthinking my code again and again. What can be good, but it’s kinda freezing the process itself.

For example, I can store 2 variables in:

  • Store them in 2 different arrays
  • Create serializable class and has 1 array in it to store them (or 2 different variables)
  • Store them in 2 different variables
  • Store them in dictionary
  • Create scriptable object to store them
  • So go on.

And if most of the time you can decide and choose more appropriate way because it’s kinda obvious sometime it is not so obvious.

My main question here - can you recommend any documentation or book which can help enrich my knowledge of perfomancing in C#, because while I can code I cannot answer simple questions:

Is it better to create scriptable object to store variable and use them? or is it better to create another class for them? or is it better to keep them in the same class?

Add boolean and function to existed script or create new one? And so go on.

By better I mean not the right way, but less expensive for computer to execute.

Maybe some of you will say that I don’t certainly need this understanding of how much memory each variable or class take, because, at least in my opinion, for perfomancing more crucial number of iterations than type of variables themselves, however I feel like I have understanding of tools and how to create with them something, but I have zero understanding of operations that are executing on the background without my participation.

No, noone can help you here because your question is way too abstract. There is no globally valid measure for “performance”. We generally distinguish between low memory footprint / less garbage collection pressure / fastest execution time / least likely cache misses and so on. All those optimisation goals have different usecases and highly depend on the usage of said “variables”.

There is no general purpose “Optimise()” method you call and everything is golden. Optimisation is always specific for a certain goal and always depends on your specific program structure and software design. You essentially ask for putting years and years of programming experience into a single answer. That’s a bit like asking for “the answer to life the universe and everything”. If that’s the case I would answer “42” due to the lack of information on the actual question.

Personally, I see the biggest difference between all of the options you listed as being intent rather than performance.

Compilers can optimize away a handful of variables. They can unroll small loops. They can inline “simple” methods. They can even condense seemingly complex methods into single assembly instructions in rare cases.

What they cannot do, is express your intent. Are these variables closely related? They should probably be in a class. Are they completely unrelated? They belong in separate classes. Do they represent some sort of sequential data? Array elements. Can/should they be used as lookup values, or will they need to be tightly bound to some indexer? Dictionary. Do you need to be able to edit them in the Unity Inspector or save multiple predefined data sets? ScriptableObjects. Do these values depend on values of other variables? You may need a property or method instead.

Only you can answer these questions. Always remember these two adages: “Keep It Simple, Stupid” and “Premature optimization is the root of all evil”.