1

I'm trying to find the most efficient way of dealing with array of sets of structs. Namely, at the moment I have got the following structure:

struct myStruct{
    double p1;
    double p2;
    bool p3;
    double w;
}

So, objects I modelling have three properties and a weight assigned to them. Now, these are arranged in arrays of fixed size, say, 10, and there are multiple combinations of the objects weights, say 1000 of them:

const int nObj = 10;
const int n = 1000;

myStruct comb[n][nObj];

And finally there are a couple of functions I'm passing 10-sized arrays to:

double evalComb(myStruct (&ms)[nObj], double q1, double q2){
    double output = 0;
    for(int i = 0; i < nObj; i++){
        output += // calculate some stuff using p1,p2,p3,w and other params
    }
    return output;
};

Now, the issue is that the set of ten values of p1, p2, p3 is fixed across all the 1000 combinations (but not const), the only thing changing is set of 10 weights w. This kind of makes me think it is a waste of memory copying all that 1000 times... I've got the working code but would like to make it quicker and my understanding is that this would be the most obvious place to optimize (as that function is called millions of times and the 90% of time goes there). Is is better to get weights out of the struct and have 2d double array of those leaving a 1d array of structs? That would mean another array parameter passed to a function, wouldn't it slow it down? Maybe I should have structs with arrays inside them instead? Any other issues which could arise with this?

5
  • Is there any reason you can't have a class with a double, that has three static members Commented Aug 5, 2013 at 0:30
  • I just think having class is a bit of an overkill - I don't need any functionality which struct can't have, also I doubt it would help performance-wise. If I'm wrong, you are welcome to answer with such alternative :) Commented Aug 5, 2013 at 0:33
  • 1
    Classes and structs are identical in c++ except for public private defaults Commented Aug 5, 2013 at 0:34
  • @aaronman Well, the question still stands then with no difference, if they are identical... Why class would be any better then? Commented Aug 5, 2013 at 0:35
  • You should create a class that manipulates these issues internally, so you don't have to have 1000 copies of every set of properties. Using simple arrays doesn't seem to be a good solution to this problem. Commented Aug 5, 2013 at 1:09

3 Answers 3

1

What I would suggest is to have a class containing a double with three static members for the things that don't change.

struct myStruct{
    static std::array<double,10> p1;
    static std::array<double,10> p2;
    static std::array<bool,10> p3;
    double w;
}  

This way you save the space and still have easy access to the three other variables, IMO it is better to keep the class instead of just using an array of doubles because it preserves the association in between the variables and also gives you an opportunity to modify the class later on. Classes don't really cause as much overhead as you think especially with modern compilers.

You may also want to make the static variables const if they will never change

Sign up to request clarification or add additional context in comments.

6 Comments

Do you mean 'static double p1[10]', etc. then? Or still an array of myStruct?
I don't understand you said p1 never changes why would you need it to be an array of ten
10 values never change across 1000 sets of 10 values. This is for p1-p3. w does change.
but is p1 different between each set of 10
There is a set of 10 (different) values for p1. This set is static (if it changes, it changes for all 1000 combinations).
|
0

If the 10 values for p1, p2, and p3 never change make them constant or use #define. You'll get a performance increase when the compiler swaps them out in your method or function definitions with literals. An "object" in this case would just be an array of 10 doubles for w. You might also consider declaring w as a 1000 X 10 array to keep the memory allocated in a contiguous block. That will give you a performance increase over mixing variable types that are not memory address aligned in your struct.

4 Comments

Edited the question - parameters are static, but not constant.
oh god please don't recommend using define in c++
I agree const is a much better option, although 30 define statements at the top of the file would look awesome. :-)
It's disturbing to see u you say that
0

If the p1, p2, p3 members are the same (common) for all of your 100 or so structures I'd like to propose a change to your design. You create a new class/structure for your properties

struct myProperties {
    // TODO: define c'tor
    double p1;
    double p2;
    bool p3;
};

struct myStruct {
      myStruct(myProperties *properties_): properties(properties_) {}
      myProperties properties;
      // TODO: define getters for p1, p2, p3
};

When creating a group of myStruct with the same properties you pass a pointer to a myProperties object with your desired properties

myProperties m(p1, p2, p3); 
myStruct s(&m);

This way you can have one property group (p1, p2, p3) assigned to one group of your 100 myStruct objects and another property group assigned to your other 1000 myStruct objects (you cannot achieve this with static members).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.