使用数组,结构或类有速度差异吗?

时间:2012-01-20 23:32:12

标签: c++ performance

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

速度是否存在差异,或者它们的行为方式是否相同?

// #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;
};

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

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