2

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;
};
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • 1
    Why not get it working first in a way that's intuitive to you? Who cares how fast an incomplete program is? – GManNickG Jan 20 '12 at 23:40
  • Don't worry about the wrong stuff. First get it working. Then profile or *[random pause](http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux/378024#378024)* to see what you need to be concerned about, if anything. – Mike Dunlavey Jan 21 '12 at 02:09

2 Answers2

7

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.

smparkes
  • 13,807
  • 4
  • 36
  • 61
1

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.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173