A few days ago I asked you a question and I have some really useful answers. I will make a summary for those of you who have not read, and I will explain my new doubts and where I have problems.
Explanation
I am working on a program that simulates a small database, which primarily reads information from txt files and stores them in computer memory, and then I can make queries using regular tables and / or transposed tables. The problem is that performance is still not enough. It runs slower than I expect. I improved it, but I think I should improve it more. I have specific moments when my program does not have good performance.
Current problem
The first problem that I have now (where my program is slower) is that I spend more time, for example, on a table with 100,000 columns and 100 rows (0.325 min, I improved it thanks to your help) than 100 000 rows and 100 columns (1.61198 min, same as before). But, on the other hand, the access time to some data is better in the second case (in a specific example, 47 seconds versus 6079 seconds in the first case) any idea why ??
Explanation
Now let me remind you how my code works (with a saved summary of my code)
First of all, I have a .txt file that mimics a database table with random strings separated by "|". Here is an example table (with 7 rows and 5 columns). I also have a transposed table
NormalTable.txt
42sKuG^uM|24465\lHXP|2996fQo\kN|293cvByiV|14772cjZ`SN|
28704HxDYjzC|6869xXj\nIe|27530EymcTU|9041ByZM]I|24371fZKbNk|
24085cLKeIW|16945TuuU\Nc|16542M[Uz\|13978qMdbyF|6271ait^h|
13291_rBZS|4032aFqa|13967r^\\`T|27754k]dOTdh|24947]v_uzg|
1656nn_FQf|4042OAegZq|24022nIGz|4735Syi]\|18128klBfynQ|
6618t\SjC|20601S\EEp|11009FqZN|20486rYVPR|7449SqGC|
14799yNvcl|23623MTetGw|6192n]YU\Qe|20329QzNZO_|23845byiP|
TransposedTable.txt ( )
42sKuG^uM|28704HxDYjzC|24085cLKeIW|13291_rBZS|1656nn_FQf|6618t\SjC|14799yNvcl|
24465\lHXP|6869xXj\nIe|16945TuuU\Nc|4032aFqa|4042OAegZq|20601S\EEp|23623MTetGw|
2996fQo\kN|27530EymcTU|16542M[Uz\|13967r^\\`T|24022nIGz|11009FqZN|6192n]YU\Qe|
293cvByiV|9041ByZM]I|13978qMdbyF|27754k]dOTdh|4735Syi]\|20486rYVPR|20329QzNZO_|
14772cjZ`SN|24371fZKbNk|6271ait^h|24947]v_uzg|18128klBfynQ|7449SqGC|23845byiP|
TXT . , . , , .
, .
, Table.txt
int h;
do
{
cout<< "Do you want to query the normal table or the transposed table? (1- Normal table/ 2- Transposed table):" ;
cin>>h;
}while(h!=1 && h!=2);
string ruta_base("C:\\Users\\Raul Velez\\Desktop\\Tables\\");
if(h==1)
{
ruta_base +="NormalTable.txt";
}
if(h==2)
{
ruta_base +="TransposedTable.txt";
}
string temp;
vector<string> buffer;
vector<ElementSet> RowsCols;
ifstream ifs(ruta_base.c_str());
while(getline( ifs, temp ))
{
size_t tokenPosition = temp.find("|");
const char* p = temp.c_str();
char* p1 = strdup(p);
char* pch = strtok(p1, "|");
while(pch)
{
buffer.push_back(string(pch));
pch = strtok(NULL,"|");
}
free(p1);
ElementSet sss(0,buffer);
buffer.clear();
RowsCols.push_back(sss);
}
Table TablesStorage(RowsCols);
vector<Table> TablesDescriptor;
TablesDescriptor.push_back(TablesStorage);
DataBase database(1, TablesDescriptor);
,
. , , . , - "n", "numTuples" "y". ( , "y", , , , 54 (00110110 ) 2, 3, 5 6). , Vector. .
if (h == 2), , : ?
,
int n, numTuples;
unsigned long long int y;
cout<< "Write the ID of the row you want to get more information: " ;
cin>>n;
cout<< "Write the number of followed tuples to be queried: " ;
cin>>numTuples;
cout<<"Write the ID of the 'columns' you want to get more information: ";
cin>>y;
unsigned int r;
int t=0;
int idTable;
vector<int> columnsToBeQueried;
vector<string> shownVector;
bitset<5000> mask;
mask=0x1;
clock_t t1, t2;
t1=clock();
bitset<5000> binaryNumber = Utilities().getDecToBin(y);
for(r=0;r<binaryNumber.size();r++)
{
if(binaryNumber.test(r) & mask.test(r))
{
columnsToBeQueried.push_back(r);
}
mask=mask<<1;
}
do
{
for(int z=0;z<columnsToBeQueried.size();z++)
{
ElementSet selectedElementSet;
int i;
i=columnsToBeQueried.at(z);
Table& selectedTable = database.getPointer().at(0);
if(h == 1)
{
selectedElementSet=selectedTable.getRowsCols().at(n);
shownVector.push_back(selectedElementSet.getElements().at(i));
}
if(h == 2)
{
selectedElementSet=selectedTable.getRowsCols().at(i);
shownVector.push_back(selectedElementSet.getElements().at(n));
}
n=n+1;
t++;
}
}while(t<numTuples);
t2=clock();
showVector().finalVector(shownVector);
float diff ((float)t2-(float)t1);
float microseconds = diff / CLOCKS_PER_SEC*1000000;
cout<<"Time: "<<microseconds<<endl;
, , :
class ElementSet
{
private:
int id;
vector<string> elements;
public:
ElementSet();
ElementSet(int, vector<string>&);
const int& getId();
void setId(int);
const vector<string>& getElements();
void setElements(vector<string>);
};
class Table
{
private:
vector<ElementSet> RowsCols;
public:
Table();
Table(vector<ElementSet>&);
const vector<ElementSet>& getRowsCols();
void setRowsCols(vector<ElementSet>);
};
class DataBase
{
private:
int id;
vector<Table> pointer;
public:
DataBase();
DataBase(int, vector<Table>&);
const int& getId();
void setId(int);
const vector<Table>& getPointer();
void setPointer(vector<Table>);
};
class Utilities
{
public:
Utilities();
static bitset<500> getDecToBin(unsigned long long int);
};
!!!:)