简体   繁体   English

在处理顺序访问的对象时,链接列表与数组在性能上的关系?

[英]Linked-lists versus array in performance when dealing with sequentially accessed objects?

I am currently designing a game in which a maximum of approximately 10,000 objects will be used per level. 我目前正在设计一款游戏,其中每个关卡最多使用10,000个对象。 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. 使用链表,您将失去在CPU级别进行预取的所有好处,因为每个节点都必须单独有效地进行获取。

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? 当然,由于这是C ++,因此您可以通过将std::vector使用换成std::list来很容易地测试性能差异,对吗? :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM