I am working on a program, modeling a small database where I can make queries, and after writing the code I completed it, but the performance is pretty poor. It works very slowly. I tried to improve it, but I started with C ++ on my own a few months ago, so my knowledge is still very low. Therefore, I would like to find a solution to increase productivity.
Let me explain how my code works. Here I put together a generalized example of how my code works.
First of all, I have a .txt file that mimics a database table with random strings separated by "|". Here you have an example table (with 5 rows and 5 columns).
Table.txt
0|42sKuG^uM|24465\lHXP|2996fQo\kN|293cvByiV
1|14772cjZ`SN|28704HxDYjzC|6869xXj\nIe|27530EymcTU
2|9041ByZM]I|24371fZKbNk|24085cLKeIW|16945TuuU\Nc
3|16542M[Uz\|13978qMdbyF|6271ait^h|13291_rBZS
4|4032aFqa|13967r^\\`T|27754k]dOTdh|24947]v_uzg
This information in a TXT file is read by my program and stored in the computer's memory. Then, when making requests, I get access to this information stored in the computer's memory. Downloading data to your computer’s memory can be a slow process, but accessing the data later will be faster, which is really important to me.
Here you have a piece of code that reads this information from a file and stores it on a computer.
Code that reads data from the Table.txt file and stores it in computer memory
string ruta_base("C:\\a\\Table.txt");
string temp;
vector<string> buffer;
vector<ElementSet> RowsCols;
ifstream ifs(ruta_base.c_str());
while(getline( ifs, temp ))
{
size_t tokenPosition = temp.find("|");
while (tokenPosition != string::npos)
{
string element;
tokenPosition = temp.find("|");
element = temp.substr(0, tokenPosition);
buffer.push_back(element);
temp.erase(0, tokenPosition+1);
}
ElementSet ss(0,buffer);
buffer.clear();
RowsCols.push_back(ss);
}
vector<Table> TablesDescriptor;
Table TablesStorage(RowsCols);
TablesDescriptor.push_back(TablesStorage);
DataBase database(1, TablesDescriptor);
. , , . , - "n", "numTuples" "y". ( , "y", , , , 54 (00110110 ) 2, 3, 5 6). , Vector. .
,
int n, numTuples;
unsigned long long int y;
clock_t t1, t2;
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<500> mask;
mask=0x1;
t1=clock();
bitset<500> 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++)
{
int i;
i=columnsToBeQueried.at(z);
vector<int> colTab;
colTab.push_back(1);
const Table& selectedTable = database.getPointer().at(0);
ElementSet selectedElementSet;
selectedElementSet=selectedTable.getRowsCols().at(n);
shownVector.push_back(selectedElementSet.getElements().at(i));
}
n=n+1;
t++;
}while(t<numTuples);
t2=clock();
float diff ((float)t2-(float)t1);
float microseconds = diff / CLOCKS_PER_SEC*1000000;
cout<<"The query time is: "<<microseconds<<" 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);
};
, , , , ( 100 100 , 10000 1000 ). , ... , ????
!!!:)