What is the most efficient way to represent small values in a struct?

前端 未结 15 837
星月不相逢
星月不相逢 2021-02-01 02:15

Often I find myself having to represent a structure that consists of very small values. For example, Foo has 4 values, a, b, c, d that, range from

15条回答
  •  礼貌的吻别
    2021-02-01 02:58

    Let's say, you have a memory bus that's a little bit older and can deliver 10 GB/s. Now take a CPU at 2.5 GHz, and you see that you would need to handle at least four bytes per cycle to saturate the memory bus. As such, when you use the definition of

    struct Foo {
        char a;
        char b;
        char c;
        char d;
    }
    

    and use all four variables in each pass through the data, your code will be CPU bound. You can't gain any speed by a denser packing.

    Now, this is different when each pass only performs a trivial operation on one of the four values. In that case, you are better off with a struct of arrays:

    struct Foo {
        size_t count;
        char* a;    //a[count]
        char* b;    //b[count]
        char* c;    //c[count]
        char* d;    //d[count]
    }
    

提交回复
热议问题