Slow istringstream with getline
<p>I have an Wavefront .obj file parser which parses data using getline and stringstream. At first when models were tiny, there was no problem, but now, when I try to load a model with ~207000 lines, only for the first pass where I count all elements it takes ridiculous amount of time (~4.7s) on high end PC and the second pass takes half a minute. Blender on the other hand loads whole model in mere 2 seconds or so. I use visual studio 2012, currently in debug mode.</p>

<p>My code for counting elements looks like this:</p>

<pre><code>istringstream input(obj);
string line;
while (getline(input, line)) {
    if (line.find("# ") != string::npos) {
        // Comments.
    }
    else if (line.find("f ") != string::npos) {
        faces++;
    }
    else if (line.find("v ") != string::npos) {
        vertices += 3;
    }
    else if (line.find("vn ") != string::npos) {
        normals += 3;
    }
    else if (line.find("vt ") != string::npos) {
        uvCoordinates += 2;
    }
    else if (line.find("o ") != string::npos) {
        // Count here, if needed.
    }
}
</code></pre>

<p>Code for actually loading whole data that takes ~30s:</p>

<pre><code>istringstream input(obj);
string line;
if (faces.capacity() &gt; UINT_MAX / 3) {
    LOGE("Model cannot have more faces than: %d", UINT_MAX / 3);
    return false;
}
while (getline(input, line)) {
    vector&lt;string&gt; arr = stringSplit(line, ' ');
    string param = arr[0];
    int params = arr.size();
    if (line.length() == 0) {
        continue;
    }

    if (arr[0] == "v") { // Vertices.
        vertices.push_back(stringToFloat(arr[1].c_str()));
        vertices.push_back(stringToFloat(arr[2].c_str()));
        vertices.push_back(stringToFloat(arr[3].c_str()));
    }
    else if (arr[0] == "vn") { // Normals.
        normals.push_back(stringToFloat(arr[1].c_str()));
        normals.push_back(stringToFloat(arr[2].c_str()));
        normals.push_back(stringToFloat(arr[3].c_str()));
    }
    else if (arr[0] == "f") { // Faces.
        if (params &lt; 4) {
            //LOGI("LINE: %s", line.c_str());
            continue;
        }
        else if (params &gt; 4) {
            LOGI("Line: %s", line.c_str());
            LOGE("Obj models must only contain triangulated faces.");
            return false;
        }
        Face face;
        parseFace(face, line);
        faces.push_back(face);
    }
    else if (arr[0] == "vt") { // UV coordinates.
        uvCoordinates.push_back(stringToFloat(arr[1].c_str()));
        uvCoordinates.push_back(stringToFloat(arr[2].c_str()));
    }
    else if (arr[0] == "mtllib") { // Material.
        material = arr[1];
    }
    else if (arr[0] == "o") { // Sub-model.
        // Separate models here, if needed.
    }
}
</code></pre>

<p>obj variable is a string containing whole file content.
Removing everything from the inside of the first loop changes nothing for the time impact.
Any ideas on how to optimize this?</p>
