Niklas R
Niklas R

Reputation: 16900

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

I need to do calculations based on a variable anmount of data, each item in the data containing 3 values. I could use an array, struct or a class to represent one of the items.

Is there any difference in speed or do they behave all the same way?

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

Upvotes: 2

Views: 748

Answers (2)

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143279

There should be definitely no difference between struct and class with public: at the beginning and I suspect there will be no difference with array as well. Not at run time.

Upvotes: 1

smparkes
smparkes

Reputation: 14083

Structs and classes are the same as far as that goes. As long as you use a constant index, all the math is done at compile time, so it shouldn't make any difference.

Upvotes: 7

Related Questions