- Home /
C# basic perfomance question
Hello,
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.
Answer by Bunny83 · Aug 27, 2019 at 12:45 PM
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.
You actually answered on my question in a way that I should probably asked the question. You said that it's generally between low memory footprint / less garbage collection pressure / fastes exectuon time and so on and for that you need to have understanding of an actual coding process background. I mean, for example, I know that I can use transform.Translate or Rigidbody.AddForce or Nav$$anonymous$$eshAgent.SetDestination and other ways to achieve something to move, but I can't answer what is fastest between them and which one generates less garbage. Because I know what are functions using for, but I have no understanding how they are achieving that.
So, my question isn't about an golden Optimise() function, but about recommendation of a documentation/book/etc. where I can learn this basics of understanding process behind coding? Correct me if my question again makes no sense ^^ Because it's feel kinda incorrect when you can code not a big, but game like rpg, shooter, whatever type u prefer, but you can't say for sure - could you improve in ur code something besides it's view. Like, for sure I can replace Vector3.Distance with .magnitude, but only because I know how to use them, but not because I has deep knowledge of understanding what stays behind them. Does that makes sense?
Answer by Kishotta · Aug 27, 2019 at 03:02 PM
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".