Search Unity

Discussion Deep Listing - List Iteration Speed

Discussion in 'Scripting' started by AnimalMan, Oct 27, 2022.

  1. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Something I wanted to share that I had noticed was

    the iteration speed of a list of integer in a for loop, is vastly superior to the iteration speed of the same integers sorted in a 2 dimensional array.
    Particularly noticeable when using instantiate inside the loop.

    It appear to be the final optimisation you can make.

    So for example if you are making a huge grid on start. And sometimes debugging using instantiate for each point. This is the point where usually a 2 dimensional list is created to manage the grid in the future.
    But here this, if you always spoke about your grid, in a 1 dimensional way, the performance will be faster.

    now I don’t know the exact difference between asking for List[A] vs List[C], but asking for list AB to instantiate at the iteration of B, can be far slower than asking List[C] instantiate for each iteration.

    the logical approach here, is to take list C and say hey I am talking about coordinate X + Size; your neighbour, rather than I am talking to coordinate [X][Y]. I believe at the end it is using a simple sum like x + size that requires less activity. While [X][Y] performs some increased, or hotter activity under the hood. Although the organisational gains of using [X][Y] are superior for the programmer, it appear the machine likes the other way best. On the test I made, I had noticed probably around 10 seconds of start up time reduced using instantiate when starting up with vertice point objects active. But I have not yet pursued the possibility of using the x + size at all levels of the code, I actually set up for a [x][y][z] to carry some data groups for me and realised I had used a 1 dimension to locate the y value of some triangles, and ran some tests using instantiate on my vertice positions and noticed the difference here. I thought hey I could cut the xyz array into an xy array using a 1 dimensional compression. And it seems like there would be benefit from this type of consideration. Though I do not think I will abandon 2 dimensional or 3 dimensional data storage for all purposes. For the immediate task of instantiating 650000 objects on startup, i believe you’ll notice the loading screen can be cut significantly shorter using the 1 dimension.
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    This is true because of CPU caching and heap allocations. What follows is an oversimplified description of what is happening in your computer to make one dimensional arrays faster:

    Arrays are heap-allocated in C#, which means that a jagged 2D array (an array of arrays) is actually an array containing a bunch of pointers to a bunch of other arrays, which then hold your data. Every time (give or take) you index into this array you need to make two hops; first to the location of the first array in memory to get the pointer and then to that location of the second array to get the data.

    A one-dimensional array has only one hop to the location of the data in memory, and the fact that all of the data of the array is adjacent in memory means that much or all of it can be read into the CPU at once, which saves even more costly memory fetches.