简体   繁体   English

使用ID和数组(向量)的性能

[英]Performance using IDs and arrays (vectors)

I have been taught at school to use database with integer IDs, and I want to know if it's also a good way to do so in C/C++. 我在学校曾教过如何使用具有整数ID的数据库,我想知道在C / C ++中这样做是否也是一种好方法。 I'm making a game, using Ogre3D, so I'd like my game code to use as few cycles as possible. 我正在使用Ogre3D制作游戏,所以我希望游戏代码使用尽可能少的周期。

This is not the exact code (I'm using vectors and it's about characters and abilities and such), but I'm curious to know if the line where I access the weight is going to cause a bottleneck or not, since I'd doing several array subscript. 这不是确切的代码(我正在使用矢量,它与角色和能力等有关),但我很想知道我访问权重的那一行是否会造成瓶颈,因为我想做几个数组下标。

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);
    }
}

I have heard that putting data into arrays is the best way to gain performance when looping over data repeatedly, I just want to know your opinion on this code... 我听说将数据放入数组是反复循环遍历数据时获得性能的最佳方法,我只想了解您对此代码的看法...

A good optimizer should be able to compute the exact offset of the memory address each of those items. 一个好的优化器应该能够计算出这些项目中每一个的内存地址的确切偏移量。 There is no dependency between loop iterations, so you should be able to get loop unrolled (SIMD processing). 循环迭代之间没有依赖性,因此您应该能够展开循环(SIMD处理)。 Looks great, IMHO. 看起来不错,恕我直言 If you can avoid floats, that will also help you. 如果您可以避免浮动,那对您也有帮助。

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

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