Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suspect that this is because of the way <code>std::vector</code> gets resized during the process of a push_back() function call. If you try using <code>std::list</code> or <code>std::vector::reserve()</code> to reserve enough space for the sentences, you should get a much better performance. Or you could use a combination of both like below for split1():</p> <pre><code>void split1(vector&lt;string&gt; &amp;tokens, const string &amp;str, const string &amp;delimiters = " ") { // Skip delimiters at beginning string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first non-delimiter string::size_type pos = str.find_first_of(delimiters, lastPos); list&lt;string&gt; token_list; while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the list token_list.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters lastPos = str.find_first_not_of(delimiters, pos); // Find next non-delimiter pos = str.find_first_of(delimiters, lastPos); } tokens.assign(token_list.begin(), token_list.end()); } </code></pre> <p><em>EDIT</em>: The other obvious thing I see is that Python variable <code>dummy</code> gets <em>assigned</em> each time but not modified. So it's not a fair comparison against C++. You should try modifying your Python code to be <code>dummy = []</code> to initialize it and then do <code>dummy += line.split()</code>. Can you report the runtime after this?</p> <p><strong>EDIT2</strong>: To make it even more fair can you modify the while loop in C++ code to be:</p> <pre><code> while(cin) { getline(cin, input_line); std::vector&lt;string&gt; spline; // create a new vector //I'm trying one of the two implementations, per compilation, obviously: // split1(spline, input_line); split2(spline, input_line); count++; }; </code></pre>
 

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