   Speeding up .obj loading
  Hi, i'm trying to write a loader for .obj models.Using cryteks improved sponza model (it's about 20mb) for testing, and so far it's working decently.But it's really slow, it takes about 15sec to load the model.When i open the model in Maya it's loaded in a sec, so I guess i must be doing something really inefficent.Here's the code I use, it dosen't save data or anything, just prints it for now.bool loadOBJ(const char* filename){ifstream file(filename);if (! file.good()) {cout << "Failed to open file" << endl;return false;}string lineStr;while (getline(file, lineStr) && file.good()) {stringstream lineStream(lineStr);// check line typestring lineType;lineStream >> lineType;if (lineType == "g") {// group namestring name;lineStream >> name;cout << "Group name: " << name << endl;} else if (lineType == "v") {// vertex, 3 positionsfloat x, y, z;lineStream >> x >> y >> z;//cout << "Vertex( " << x << ", " << y << ", " << z << " )" << endl;} else if (lineType == "vn") {// vertex normal, 3 positionsfloat x, y, z;lineStream >> x >> y >> z;//cout << "Normal( " << x << ", " << y << ", " << z << " )" << endl;} else if (lineType == "f") {// face, 3 or 4 edgesint pos[4];int tex[4];int norm[4];int edgeCount = 0;string str;while (lineStream >> str) {stringstream ss(str);int data[3];string s;int index = 0;while (getline(ss, s, '/')) {data[index++] = atoi(s.c_str());}pos[edgeCount] = data[0];tex[edgeCount] = data[1];norm[edgeCount] = data[2];edgeCount++;} //cout << "Face: " << pos[0] << "/" << tex[0] << "/" << norm[0] << " ";//cout << pos[1] << "/" << tex[1] << "/" << norm[1] << " ";//cout << pos[2] << "/" << tex[2] << "/" << norm[2] << endl;}//cout << lineStream.str() << endl;}return true;}ps.i've commented out all cout's except the group name sence i know that's a major speedkill.   
  Without having analyzed your code:You can take a look into my obj-loader, which is implemented in Assimp:Assimp-link . I heard some good things about its performance.And you can use a profiler like CodeAnalyst to see where are your bottlenecks in your parser.Kimmi   ;  Second the suggestion that you look at it with a profiler.My guess is that you're spending a lot of time constructing and handling string and stringstream objects. Maya probably doesn't use them; it probably maps the file straight into memory, then scans through byte-by-byte using a finite state machine to parse everything.   ;Quote:But it's really slow, it takes about 15sec to load the model.Are you testing a release build, or a debug build?   ;  Thanks for all replies!@KimmiI can't seem go get CodeAnalyst installed. Got any other good profiler to recommend?@superpigI'll look in to that, i thought stringstream constructors we're fast.@jykIt's on release build, debug is just slightly slower   ;  Rather than read the file line by line, would it be quicker to load the whole file into memory via a binary read and then pull stringstreams out of that buffer?It seems like it would be slow to hit the hard drive over and over, although I don't know if C++ already performs some caching behind the scenes.   ;  You might want to consider using a more compact and easy to read format for your final baked assets.That is assuming that this loader is for the end application and not some intermediary tool, of course.   ;  If you are using Linux you can try gprof. If you are using Windows I heard that LTProf works fine ( but I haven't tried it until now ): Prductpage LTProf .Kimmi   ;Quote:Original post by JBlixt@superpigI'll look in to that, i thought stringstream constructors we're fast.Not constructing something is always faster than constructing something. ;) Allocating memory takes some time, so try to avoid it if you don't need it.   