Hello. What is faster and more optimal? One large List<T> (5,000 elements so) or several List<T> 's smaller (500,1000 elements, etc.)

  • Unity3D has nothing to do with it (although ...), and several sheets seem to be better, although it needs to be tested. - SmInc
  • one
    Optimal for which operations? - Andrey NOP
  • I use the List for Object Pooling gaming prefabs - Scatman
  • I would recommend Dec - ishidex2

1 answer 1

The question has little to Unity .

Here, the role is played only by c# , in which List<T> , as far as I know, is a simple wrapper for an array.

In fact, it only offers a user-friendly interface that transparently for the user expands the array, inserts, deletes elements, and so on.

For everyday tasks, use one sheet optimally, of course. If you think that this is equivalent лучше распихать предметы в две сумки, а то одна может порваться , then you are mistaken, because in such an array (collection) access to the element is made by index , and regardless of the length of the array (list) you will get this item for the same time .

Of course, if you are going to frequently insert elements into the middle of the list ( Insert() ) or delete from the list ( Remove() ), the option with several collections will reduce the number of copies / allocations of memory and, accordingly, will give an increase in performance, but, again In such cases it is already better to use LinkedList<T> , which can give a significant performance boost.

All of the above applies only to general cases, and I do not guarantee the accuracy of this information for the case you are asking about, since I simply have no idea what your situation specifically is for .

  • @Scatman Well, in fact, for me it is still not very specific, but if, say, you want to bullet some bullets, and at the same time these same bullets are only of one kind, then use one list. If you need to fire objects of a different type, then everything depends on the situation, but I would make a list of key-value (in c #, you’ve got the dictionary called), where the key is the type of object, and the value is List , which is a pool of objects of the corresponding type. - selya
  • Yes, I probably need a Dictionary, I have two different objects with different logic in List <T> - Scatman