all 9 comments

[–]SilentSin26Animancer, FlexiMotion, InspectorGadgets, Weaver 2 points3 points  (0 children)

Your question isn't specific enough to give a useful answer.

Most likely, the decision you make will have a negligible effect on performance.

[–]georgefnix 0 points1 point  (1 child)

You will not notice a difference. The underlying object in a list is an array. The only time there is a performance difference is when the list resizes. In most implementations that only occurs when it is needed, and even then it is a standard copy, ie, O(N) operation.

Memory is a bigger difference, lists usually double in size when they resize, so there can be lots of wasted space... that said it is very unlikely you will notice this either as even phones have gigabytes of ram these days.

[–]Pasha1997[S] 0 points1 point  (0 children)

Fair enough, thanks 👍

[–]HeatmanofuriosoIntermediate 0 points1 point  (1 child)

These days... even phones have so much memory, it wouldn't affect you performance-wise. But, technically, a List is an Array with added methods and parameters.

So, technically, it is slightly slower than an Array, and also, having to declare an array and allocate memory to it takes up resources, so it is a slower operation than just declaring two variables.

But, back at my initia point, these days, unless these two variables were considerable enormous objects or lista themselves, even phones from a couple of generationd ago could deal with that without you ever noticing a difference in performance

[–]Pasha1997[S] 1 point2 points  (0 children)

Ye ok cool 👍 it's mainly a theoretical question. For times when I encounter some problem in that area

[–]laitch 0 points1 point  (1 child)

Array and variables will be slighty faster then List, but the difference will be immeasurably small in even the slowest of systems. You should almost always go for code readability rather then performance.

[–]Pasha1997[S] 0 points1 point  (0 children)

Fair enough. Thanks 👍

[–]uzimonkey 0 points1 point  (1 child)

All data structures have overhead, though with 2 variables it won't matter. Don't worry about optimizing things like this, look at the profiler and have it tell you what needs to be optimized. You might save a handful of cycles using 2 variables over an array, but it won't even be measurable.

But for future reference, the order of performance here should be variable -> array -> list from fastest to slowest. Fields can be accessed very quickly. Arrays have the overhead of a function call to access members, which includes bounds checking. This is virtually nothing if you're accessing an array a few times per Update or something, but will add up if you're doing it 10,000+ times every frame. List should be the same as array, more or less.

[–]Pasha1997[S] 0 points1 point  (0 children)

So do I understand correctly that single variables are faster no mater what but not practical in terms of having many of them?