使用数组、结构或类是否存在速度差异

Is there a speed difference in using an array, struct or class?

本文关键字:存在 是否 速度 数组 结构      更新时间:2023-10-16

我需要根据变量和数据量进行计算,数据中的每个都包含3个值。我可以使用数组结构来表示其中一个项。

速度有什么不同吗?或者他们的行为方式都一样吗?

// #1: Only arrays
typedef int triple[3];
// #2: Using a struct
struct triple {
    int a;
    int b;
    int c;
};
// #3: Using a class
class triple {
public:
    int a;
    int b;
    int c;
};

结构和类是一样的。只要你使用一个常量索引,所有的数学运算都是在编译时完成的,所以它应该没有任何区别。

structclass之间肯定没有区别,public:在一开始,我怀疑数组也不会有区别。不在运行时。