Note that there are some explanatory texts on larger screens.

plurals
  1. POChar* vs String Speed in C++
    text
    copied!<p>I have a C++ program that will read in data from a binary file and originally I stored data in <code>std::vector&lt;char*&gt; data</code>. I have changed my code so that I am now using strings instead of char*, so that <code>std::vector&lt;std::string&gt; data</code>. Some changes I had to make was to change from <code>strcmp</code> to <code>compare</code> for example. </p> <p>However I have seen my execution time dramatically increase. For a sample file, when I used char* it took 0.38s and after the conversion to string it took 1.72s on my Linux machine. I observed a similar problem on my Windows machine with execution time increasing from 0.59s to 1.05s. </p> <p>I believe this function is causing the slow down. It is part of the converter class, note private variables designated with<code>_</code> at the end of variable name. I clearly am having memory problems here and stuck in between C and C++ code. <em>I want this to be C++ code, so I updated the code at the bottom.</em></p> <p>I access <code>ids_</code> and <code>names_</code> many times in another function too, so access speed is very important. <strong>Through the use of creating a <code>map</code> instead of two separate vectors, I have been able to achieve faster speeds with more stable C++ code. Thanks to everyone!</strong></p> <h2>Example NewList.Txt</h2> <pre><code>2515 ABC 23.5 32 -99 1875.7 1 1676 XYZ 12.5 31 -97 530.82 2 279 FOO 45.5 31 -96 530.8 3 </code></pre> <h2>OLD Code:</h2> <pre><code>void converter::updateNewList(){ FILE* NewList; char lineBuffer[100]; char* id = 0; char* name = 0; int l = 0; int n; NewList = fopen("NewList.txt","r"); if (NewList == NULL){ std::cerr &lt;&lt; "Error in reading NewList.txt\n"; exit(EXIT_FAILURE); } while(!feof(NewList)){ fgets (lineBuffer , 100 , NewList); // Read line l = 0; while (!isspace(lineBuffer[l])){ l = l + 1; } id = new char[l]; switch (l){ case 1: n = sprintf (id, "%c", lineBuffer[0]); break; case 2: n = sprintf (id, "%c%c", lineBuffer[0], lineBuffer[1]); break; case 3: n = sprintf (id, "%c%c%c", lineBuffer[0], lineBuffer[1], lineBuffer[2]); break; case 4: n = sprintf (id, "%c%c%c%c", lineBuffer[0], lineBuffer[1], lineBuffer[2],lineBuffer[3]); break; default: n = -1; break; } if (n &lt; 0){ std::cerr &lt;&lt; "Error in processing ids from NewList.txt\n"; exit(EXIT_FAILURE); } l = l + 1; int s = l; while (!isspace(lineBuffer[l])){ l = l + 1; } name = new char[l-s]; switch (l-s){ case 2: n = sprintf (name, "%c%c", lineBuffer[s+0], lineBuffer[s+1]); break; case 3: n = sprintf (name, "%c%c%c", lineBuffer[s+0], lineBuffer[s+1], lineBuffer[s+2]); break; case 4: n = sprintf (name, "%c%c%c%c", lineBuffer[s+0], lineBuffer[s+1], lineBuffer[s+2],lineBuffer[s+3]); break; default: n = -1; break; } if (n &lt; 0){ std::cerr &lt;&lt; "Error in processing short name from NewList.txt\n"; exit(EXIT_FAILURE); } ids_.push_back ( std::string(id) ); names_.push_back(std::string(name)); } bool isFound = false; for (unsigned int i = 0; i &lt; siteNames_.size(); i ++) { isFound = false; for (unsigned int j = 0; j &lt; names_.size(); j ++) { if (siteNames_[i].compare(names_[j]) == 0){ isFound = true; } } } fclose(NewList); delete [] id; delete [] name; } </code></pre> <h2>C++ CODE</h2> <pre><code>void converter::updateNewList(){ std::ifstream NewList ("NewList.txt"); while(NewList.good()){ unsigned int id (0); std::string name; // get the ID and name NewList &gt;&gt; id &gt;&gt; name; // ignore the rest of the line NewList.ignore( std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); info_.insert(std::pair&lt;std::string, unsigned int&gt;(name,id)); } NewList.close(); } </code></pre> <p>UPDATE: Follow up question: <a href="https://stackoverflow.com/q/3992548/363829">Bottleneck from comparing strings</a> and thanks for the very useful help! I will not be making these mistakes in the future!</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