Search Unity

Which type of "Array" (Built-in array, generic list, etc.) is the least expensive?

Discussion in 'Scripting' started by FuzzyQuills, Dec 19, 2014.

  1. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Hi! :)

    I have a problem... :D

    Basically, I need to find a cheap and fast way to sort 6-10 cars in ranking order. At the moment, I use a generic list. (I used an array before, but that proved to be too slow.)

    So what, in any way would be the least expensive array-type class to sort? I initially thought generic lists would be better, but, although there is a performance improvement there, the frame rate still stutters on my pendo pad... :(
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    If you're only sorting 6-10 items, any solution would do (and I would certainly go with the easiest one, i.e. List).

    A rule of thumb is that if lists (or arrays) are posing a problem in your game, you have more serious problems. That might not always be true, though, but in general it is.

    I suggest you post the relevant code so that we can see the whole thing in context.
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Sorting such a low amount shouldn't cause any problems, unless you really chose a weird and complex way to do so.
    Like @toreau said, post some more information about how, when and where you actually sort them.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For frequent sorting I would tend to recommend a SortedList, or a similar collection that keeps its members sorted automatically.

    For 6-10 references you can use whatever you want. As indicated there are other problems causing the performance issues. My microwave is powerful enough to keep 6-10 items sorted. I would suggest running your app through the profiler and finding out what is really going wrong.
     
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @BoredMormon:
    Yea... Only I don't have Unity pro! :( (Built-in profiler only helped me find it...)

    @Suddoha, @toreau:
    Here, I'll explain how my game sorts the cars. (Yes, it's a racing game! :))

    Basically, the rankings are influenced by three things: current lap, current way point, current way point distance. by comparing each one, if they're the same, it checks it more accurately with another measurement. if trying to check both current lap and waypoint fails with the same number, then the distance to the next one is used. (This never fails!)

    Could the problem be the fact that I am comparing THREE values instead of one, like most sorting codes? o_O Oh, and being a ranking system, it needs to be done every second/third/fourth frame or so, otherwise, a car in first looks like it's in 5th... :D

    Also, could some AI code being executed in FixedUpdate be the problem? The other obvious bottleneck is PhysX... :D
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Even with all that, I suspect the problem might be the result of a severe inefficiency. As here has pointed out, loops concerning 6-10 iterations should take a negligent amount of time to execute.

    That being said, if you have a great deal of nested loops to make your calculation, it's surprisingly easy for your code to take exponentially unnecessary amounts of time to solve.

    Post the relevant sorting function, so we can take a look.
     
  7. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Sure! Here it is:
    Code (csharp):
    1. function UpdateRanks_Laps (a : System.Object, b : System.Object)
    2. {
    3.         if ( !(a instanceof Rank_Sys) || !(b instanceof Rank_Sys)) return;
    4.         var CarA : Rank_Sys = a;
    5.         var CarB : Rank_Sys = b;
    6.  
    7.         return CarB.lap.CompareTo(CarA.lap) || CarB.currentWaypoint.CompareTo(CarA.currentWaypoint) || CarA.dist.CompareTo(CarB.dist);
    8. //        }
    9. }
    Anything wrong here? Note that this is used with a generic list.
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    And I am so sure I saw something else that's cheaper than CompareTo, but I can't remember what it was... :D