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…
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.
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.
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.
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? 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…
Also, could some AI code being executed in FixedUpdate be the problem? The other obvious bottleneck is PhysX…
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.