Monday, October 15, 2007

C++ Optimizing

Been speeding up the loading and processing code for Democracy 2 today.
Here's some tips.

this:


std::list::iterator it;
for(it = list.begin(); it != list.end(); ++it)
{
}

Is way slower than

std::list::iterator it = list.begin();
std::list::iterator endit = list.end();
for(; it != endit; ++it)
{
}


mainly because end() is very slow. if you know the list itself won't change during iteration, its well worth doing.

Also, this stuff is mega slow:

ftell()
fseek()
fgetc()

Much better to laod the whole save game file (2.5 meg) into a buffer and handle the extraction of each byte yourself. its 50% faster so far, and that's without getting geekier and inlining stuff.
Hopefully the game will start up, process and laod really fast, even for old PCs. That's not because its simple, but because I enjoy optimising the cr*p out of my code :D.