Problems with query time when accessing to information stored in the computer memory
<p>I am working on a simple program, simulating a small database. And I have a strange problem when reading information, stored in the computer memory. The thing is that the query time is much higher that what was supposed and I can't see why. Let me explain my problem in detail, and in the end, you will find my question in capital letters.</p>

<p>First of all I have a .txt file simulating a database table with random strings separated with "|". Here you have an example of table (with 5 rows and 5 columns).</p>

<p><em><strong>Table.txt</em></strong></p>

<pre><code>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
</code></pre>

<p>This information in a .txt file is read by my program and stored in the computer memory. Here you have the part of the code that read this information from a file and store in the computer.</p>

<p><em><strong>Code that reads data from the Table.txt file and store it in the computer memory</em></strong></p>

<pre><code>string ruta_base("C:\\a\\Table.txt"); // Folder where my "Table.txt" is found

string temp; // Variable where every row from the Table.txt file will be firstly stored
vector&lt;string&gt; buffer; // Variable where every different row will be stored after separating the different elements by tokens.
vector&lt;ElementSet&gt; RowsCols; // Variable with a class that I have created, that simulated a vector and every vector element is a row of my table

ifstream ifs(ruta_base.c_str());

while(getline( ifs, temp )) // We will read and store line per line until the end of the ".txt" file. 
{
    size_t tokenPosition = temp.find("|"); // When we find the simbol "|" we will identify different element. So we separate the string temp into tokens that will be stored in vector&lt;string&gt; buffer

    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); // We store all the elements of every row (stores as vector&lt;string&gt; buffer) in a different position in "RowsCols" 
}

vector&lt;Table&gt; TablesDescriptor;

Table TablesStorage(RowsCols);
TablesDescriptor.push_back(TablesStorage);

DataBase database(1, TablesDescriptor);
</code></pre>

<p>After this, comes the <strong>IMPORTANT PART</strong>. Let's suppose that I want to make a query, and I ask for input. Let's say that my query is row "n", and also the consecutive tuples "numTuples", and the columns "y". (We must say that the number of columns is defined by a decimal number "y", that will be transformed into binary and will show us the columns to be queried, for example, if I ask for columns 54 (00110110 in binary) I will ask for columns 2, 3, 5 and 6). Then I access to the computer memory to the required information and store it in a vector shownVector. Here I show you the part of this code.</p>

<p><em><strong>Code that access to the required information upon my input</em></strong></p>

<pre><code>int n, numTuples; 
unsigned long long int y;
clock_t t1, t2;

cout&lt;&lt; "Write the ID of the row you want to get more information: " ;
cin&gt;&gt;n; // We get the row to be represented -&gt; "n"

cout&lt;&lt; "Write the number of followed tuples to be queried: " ;
cin&gt;&gt;numTuples; // We get the number of followed tuples to be queried-&gt; "numTuples"

cout&lt;&lt;"Write the ID of the 'columns' you want to get more information: ";
cin&gt;&gt;y; // We get the "columns" to be represented ' "y"

unsigned int r; // Auxiliar variable for the columns path
int t=0; // Auxiliar variable for the tuples path
int idTable;

vector&lt;int&gt; columnsToBeQueried; // Here we will store the columns to be queried get from the bitset&lt;500&gt; binarynumber, after comparing with a mask
vector&lt;string&gt; shownVector; // Vector to store the final information from the query
bitset&lt;500&gt; mask;
mask=0x1;

t1=clock(); // Start of the query time

bitset&lt;500&gt; binaryNumber = Utilities().getDecToBin(y); // We get the columns -&gt; change number from decimal to binary. Max number of columns: 5000

// We see which columns will be queried
for(r=0;r&lt;binaryNumber.size();r++) //
{               
    if(binaryNumber.test(r) &amp; mask.test(r))  // if both of them are bit "1"
    {
        columnsToBeQueried.push_back(r);
    }
    mask=mask&lt;&lt;1;   
}

do
{
    for(int z=0;z&lt;columnsToBeQueried.size();z++)
    {
        int i;
        i=columnsToBeQueried.at(z);

        vector&lt;int&gt; colTab;
        colTab.push_back(1); // Don't really worry about this

        //idTable = colTab.at(i);   // We identify in which table (with the id) is column_i
        // In this simple example we only have one table, so don't worry about this

        Table selectedTable = database.getPointer().at(0); // It simmulates a vector with pointers to different tables that compose the database, but our example database only have one table, so don't worry            ElementSet selectedElementSet;

        ElementSet selectedElementSet;

        selectedElementSet=selectedTable.getRowsCols().at(n);
        shownVector.push_back(selectedElementSet.getElements().at(i)); // We save in the vector shownVector the element "i" of the row "n"

    }   
    n=n+1;
    t++;            

}while(t&lt;numTuples);

t2=clock(); // End of the query time

float diff ((float)t2-(float)t1);
float microseconds = diff / CLOCKS_PER_SEC*1000000;

cout&lt;&lt;"The query time is: "&lt;&lt;microseconds&lt;&lt;" microseconds."&lt;&lt;endl;
</code></pre>

<p>So my question is... <em><strong>why query time is so different depending on the table size??? (it has nothing to do a table with 100 rows and 100 columns, and a table with 10000 rows and 1000 columns). The thing is that when i access to the information, already saved in the computer memory, i access directly to the element I am looking for and not to all the table... so the size of the table is not taken into account and query time should be the same for every query....</strong></em></p>

<p>Thank you very much for all your help!!! :)</p>

<p><em><strong>Class definitions</strong></em></p>

<p>As some of you asked for, I add the definition of the classes Table and ElementSet:</p>

<pre><code>class ElementSet
{
private:
    int id;
    vector&lt;string&gt; elements; 

public:
    ElementSet(); 
    ElementSet(int, vector&lt;string&gt;); 

    int getId();
    void setId(int);

    vector&lt;string&gt; getElements();
    void setElements(vector&lt;string&gt;);

};

class Table
{
private:
    vector&lt;ElementSet&gt; RowsCols; 

public:
    Table(); 
    Table(vector&lt;ElementSet&gt;); 

    vector&lt;ElementSet&gt; getRowsCols();
    void setRowsCols(vector&lt;ElementSet&gt;);
};


class DataBase
{
     private:
        int id;
        vector&lt;Table&gt; pointer; 

     public:
        DataBase(); 
        DataBase(int, vector&lt;Table&gt;); 

    int getId();
    void setId(int);

    vector&lt;Table&gt; getPointer();
    void setPointer(vector&lt;Table&gt;);

    };

class Utilities
{
        public:
        Utilities();
        static bitset&lt;500&gt; getDecToBin(unsigned long long int);
};
</code></pre>
