I am currently designing a game in which a maximum of approximately 10,000 objects will be used per level. Each frame all of the objects will be accessed in a sequential order at least once, sometimes twice or more. Right now I am using arrays but I am very curious as to if linked-lists would be better suited to handle such a task.
I've never used linked-lists but this seems like an applicable time to use them, and since the project I'm working on is a learning-one as much as anything else, I would like to try a new approach to handling the objects in the game. Linked lists seem like a good way to save space and speed the game up, but due to my inexperience I am curious as to if there will be noticeable performance lost by switching to linked-lists. (Normally my motto is to try it and see, but in this case it requires a fair amount of work to go about implementing linked lists over arrays, and I've spent a lot of time on开发者_如何学编程 designing how the levels and objects will work already, so I'm a bit sick of it at the moment.)
Unless you are very frequently inserting and removing elements in the middle of a sequence, an array will all but certainly outperform a linked list (though, there are a handful of scenarios in which linked lists are useful).
With a linked list, you lose all the benefits of prefetching at the CPU level, since each node effectively has to be fetched individually.
Of course, since this is C++, you can very easily test the performance difference by swapping out your usage of std::vector
with std::list
, right? :-)
精彩评论