Hi:
I need to write a program that essentially needs to mane a list of a list of a list (that's right 3 times) of a certain class I created. I was having some efficiency problems so I decided to change all of the accesses to the structure form structure[i][j][k] to using the .at() function. I wrote a little code just to test this:
This is the Class named Tile.h
class Tile
{
public:
Tile(qreal p, QList<quint16>);
Tile();
//Definition of basic operations
bool operator == (Tile t);
//Definition of sets and getters.
QList<quint16> Indexes(){return indexes;}
qreal param(){return parameter;}
void setParam(qreal p){parameter = p;}
void setIndexes(QList<quint16> ind){indexes = ind;}
//Function use Mainly for Debugging
virtual ~Tile();
private:
qreal parameter;
QList<quint16> indexes;
};
class Tile
{
public:
Tile(qreal p, QList<quint16>);
Tile();
//Definition of basic operations
bool operator == (Tile t);
//Definition of sets and getters.
QList<quint16> Indexes(){return indexes;}
qreal param(){return parameter;}
void setParam(qreal p){parameter = p;}
void setIndexes(QList<quint16> ind){indexes = ind;}
//Function use Mainly for Debugging
QString toString();
virtual ~Tile();
private:
qreal parameter;
QList<quint16> indexes;
};
To copy to clipboard, switch view to plain text mode
And the Tile.cpp
Tile::Tile(qreal p, QList<quint16> ind){
indexes = ind;
parameter = p;
elegibility = 0;
}
Tile::Tile(){
parameter = 0;
}
/****************Operators**************************/
bool Tile::operator == (Tile t){
QList<quint16> comp = t.Indexes();
bool res = true;
for (int i = 0; i < comp.size(); i++){
if (comp.at(i) != indexes.at(i)){
res = false;
break;
}
}
return res;
}
/***********toString******************/
for (int i = 0; i < indexes.size(); i++){
ws
= ws
+ " " + QString::number(indexes.
at(i
));
}
if(indexes.size() == 0){
ws
= "Nothing to show: Value is: " + QString::number(parameter
);
}
return ws;
}
Tile::~Tile(){
}
Tile::Tile(qreal p, QList<quint16> ind){
indexes = ind;
parameter = p;
elegibility = 0;
}
Tile::Tile(){
parameter = 0;
}
/****************Operators**************************/
bool Tile::operator == (Tile t){
QList<quint16> comp = t.Indexes();
bool res = true;
for (int i = 0; i < comp.size(); i++){
if (comp.at(i) != indexes.at(i)){
res = false;
break;
}
}
return res;
}
/***********toString******************/
QString Tile::toString(){
QString ws = "";
for (int i = 0; i < indexes.size(); i++){
ws = ws + " " + QString::number(indexes.at(i));
}
if(indexes.size() == 0){
ws = "Nothing to show: Value is: " + QString::number(parameter);
}
return ws;
}
Tile::~Tile(){
}
To copy to clipboard, switch view to plain text mode
Then I wrote this code in a constructor just to test using this
int Im = 5;
int Jm = 10;
int Km = 6000;
for (int i = 0; i < Im; i++){
for(int j = 0; j < Jm; j++){
QVector<Tile> temp;
for (int k = 0; k < Km; k++){
QList<quint16> ind;
ind << k;
Tile t(0,ind);
temp << t;
}
temp2 << temp;
}
test << temp2;
}
int Im = 5;
int Jm = 10;
int Km = 6000;
for (int i = 0; i < Im; i++){
QVector < QVector<Tile> > temp2;
for(int j = 0; j < Jm; j++){
QVector<Tile> temp;
for (int k = 0; k < Km; k++){
QList<quint16> ind;
ind << k;
Tile t(0,ind);
temp << t;
}
temp2 << temp;
}
test << temp2;
}
To copy to clipboard, switch view to plain text mode
Where test is defined by:
QVector < QVector< QVector<Tile> > > test;
QVector < QVector< QVector<Tile> > > test;
To copy to clipboard, switch view to plain text mode
So then I write this line in a button action just to see if it would compile
test.at(2).at(5).at(3940).setParam(test.at(2).at(5).at(3940).param() + 15.0);
test.at(2).at(5).at(3940).setParam(test.at(2).at(5).at(3940).param() + 15.0);
To copy to clipboard, switch view to plain text mode
And it doesn't and I get these two errors
passing ‘const Tile’ as ‘this’ argument of ‘qreal Tile:
aram()’ discards qualifiers
passing ‘const Tile’ as ‘this’ argument of ‘void Tile::setParam(qreal)’ discards qualifiers
So I change it to this in order for it to work
test[2][5][3940].setParam(test[2][5][3940].param() + 15.0);
test[2][5][3940].setParam(test[2][5][3940].param() + 15.0);
To copy to clipboard, switch view to plain text mode
And this works but according to the Qt documentation [] is a lot more inefficient that at(). So I want it to change it to see if my algorithm would go a bit faster.
So my question is are there any suggestion on how to make this a bit more efficient to see if I can shave off some minutes to the my algortihms runs or am I just stuck with the slow program. Or maybe on how could I manage a list of a list of a list of classes in a more efficient way.
Thanks for any help
Bookmarks