QVector with struct - performance + best practise

4.4k views Asked by At

I'm having a set of approximately 150 variables that can have different type (double, bool, array). This set of variables I need to pack into a QVector. Because of type variables vary I thought of using QVariant. So far this is my design. What I'm not sure is the performance + the search mechanism of myVariables.

enum class VariableEnums{
  VAR1,
  VAR2,
  VAR3
};

struct myVariable{
  VariableEnums var;
  QVariant value;
};

QVector<myVariable> myVariables;

The idea is that myVariables will be send to several modules. Each module will find a specific variable and update some other variable. So one module can update variable that will be used by another module. So far regarding searching a specific variable I can think only following:

foreach(myVariable *myVar, myVariables{
   if(myVar->var == VariableEnums::VAR1){
    //......
    continue;
   }
}

Is there some more effective way to do this? I could use indexOf, but I'm not sure how to use it with a structure.

thanks, m.

2

There are 2 answers

0
miro On BEST ANSWER

after doing some profiling it turned out that QVector with a struct of enums was the fastest combination. QVector

I have tried QHash, QMap and for data holders QString and QVariant

benchmark:

...searching in dataset size:  1090000 
testing < QVector<struct enum> > case time (ms): 242 
testing < QVector<struct QString> > case time (ms): 1220 
testing < QMap<enum, QVariant> > case time (ms): 3458 
testing < QHash<enum, QVariant> > case time (ms): 2167 
2
code_fodder On

you could consider using multimap instead of QVector. With multimap you can map your variables like:

Crude representation:

{
    VAR1, a
    VAR1, b
    VAR2, c
    VAR1, d
    VAR3, e
    etc...
}

Then you can use multimap::equal_range() function to get a set of variables with the same "key", e.g. variables with key "VAR1".

I am not sure if there is a QMultimap, but there is definatley a QMap I have not looked. But if not just use the standard c++ multimap.