Note that there are some explanatory texts on larger screens.

plurals
  1. POhidden copy in STL vector?
    text
    copied!<p>I am experiencing unexpected performance degradation when using vectors with classes.</p> <p>I have a csv_File class that reads in a csv file and stores the contents in a 2D vector. There's a member function that allows access, e.g.</p> <pre><code> csv_File file("file.csv"); file.access(2,2); </code></pre> <p>To access the 2,2 element.</p> <p>Then, I have another class csv_Array that stores multiple csv_File objects in a vector, e.g private member vector There's a member function that allows access, i.e. it returns a csv_File object, for example:</p> <pre><code> csv_Array file_array(5); //store 5 csv_File objects file_array.grab(0).access(2,2); </code></pre> <p>In the second line, grab returns a csv_File object (in this case, the first one) and access is a member function of the csv_File object.</p> <p>However, I have noticed that the call csv_Array.grab(0).access(2,2); is much slower than it should be (it should be just 3 vector::at calls).</p> <p>Is there some sort of hidden copy going on that is making this very slow?</p> <p>EDIT: Here are some of the relevant function prototypes:</p> <pre><code> //Access in csv_File std::string access(int row, int column); //grab in csv_Array and csv_Analysis (mentioned below) csv_File grab(int index); </code></pre> <p>ADDITIONAL DETAILS (if necessary): The application of this code is to load a bunch of csv files into memory before passing it to a friend class that will do some calculations with the data. Schematically, I have the following:</p> <p>1) csv_Array has private member vector storage; 2) csv_Analysis is a class that is a friend of csv_Array 3) csv_Analysis accesses vector storage, which is in csv_Array 4) This access is done by passing csv_Analysis a reference to storage in csv_Array (so no copy hopefully....), e.g public: csv_Analysis(csv_Array &amp;csv_block);</p> <p>Thus, the call given above[file_array.grab(0).access(2,2); ] actually has one additional class "level" in between and is more like</p> <pre><code> csv_Analysis analysis_Object(file_array); analysis_Object.grab(0).access(2,2); </code></pre> <p>where grab acts in the same way, and is also defined as a member function of csv_Analysis class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload