[英]Using arrays or std::vectors in C++, what's the performance gap?
[英]Performance using IDs and arrays (vectors)
我在学校曾教过如何使用具有整数ID的数据库,我想知道在C / C ++中这样做是否也是一种好方法。 我正在使用Ogre3D制作游戏,所以我希望游戏代码使用尽可能少的周期。
这不是确切的代码(我正在使用矢量,它与角色和能力等有关),但我很想知道我访问权重的那一行是否会造成瓶颈,因为我想做几个数组下标。
struct item
{
float weight;
int mask;
item(): mask(0) {}
}
items[2000];
struct shipment
{
int item_ids[20];
}
shipments[10000];
struct order
{
int shipment_ids[20];
}
orders[3000];
int main()
{
// if I want to access an item's data of a certain order, I do:
for (int i = 0; i < 3000; ++ i)
{
if (items[shipments[orders[4].shipment_ids[5]]].weight > 23.0)
s |= (1<< 31);
}
}
我听说将数据放入数组是反复循环遍历数据时获得性能的最佳方法,我只想了解您对此代码的看法...
一个好的优化器应该能够计算出这些项目中每一个的内存地址的确切偏移量。 循环迭代之间没有依赖性,因此您应该能够展开循环(SIMD处理)。 看起来不错,恕我直言 如果您可以避免浮动,那对您也有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.