I wrote an obj wavefront parser class to import obj models into my OpenGL projects. I tested the class in debug mode and found that it was unbearably slow.
The code works, and I did the obvious tricks to make sure it was as efficient as reasonably practical.
Still loading the test file, the 12 MB obj file, which runs up to 330,000 lines of text, took more than a minute to parse.
Frustrated, I had Google, and, of course, I was not the first to encounter this problem .
This guy who posted his request to gamedev.net just ran his algorithm in release mode, outside of the Visual Studio IDE and whammo, acceptable performance. It also worked for me, my 70 seconds were shortened to ~ 3 seconds.
I did some profiling of the algorithm, and the bottlenecks were in the calls to std :: getline and in the following:
sstream >> sToken;
Where sstream is std :: stringstream and sToken is std :: string (with reserved space).
Question
IDE ( ) - -, , IDE (F5 - )? . IDE / IDE, - ?
, , , ( , , , , , ..)
sLineBuffer.reserve( 100 );
sToken.reserve(10);
while( sstream.good() )
{
sstream >> sToken;
getline( sstream, sLineBuffer );
if( sToken.compare("f") == 0 )
nFaces ++;
else if( sToken.compare("v") == 0 )
nVertices ++;
else if( sToken.compare("vn") == 0 )
nNormals ++;
else if( sToken.compare("vt") == 0 )
nTextures ++;
else if( sToken.compare("g") == 0 )
nGroups ++;
}
m_Vertices.reserve( nVertices );
m_Normals.reserve( nNormals );
m_TexCoords.reserve( nTextures );
m_Faces.reserve( nFaces );
m_Groups.reserve( nGroups );
(~ 8 ~ 0,3 IDE), ( ~ 180 ~ 60 ).
, :
fstream stream;
stringstream sstream;
stream.open( m_sFilename.c_str(), std::ios::in );
sstream << stream.rdbuf();
stream.close();
, , std:: strings , :
sLineBuffer.reserve( 100 );
sToken.reserve(10);