Re: Whats going onto the stack here?
Ok, youve made your point, but id like some clarification on something
before I change to using std::list
I assume it would be possible to do this
struct SOMEOBJECT
{
// stuff
};
list<SOMEOBJECT> objectlist;
I'd like to know how its linking those objects together. Is it linking those
objects directly to each other or is it creating a list of SOMEOBJECT
pointers. If you say directly then how is it doing it. i.e is it creating a
container which contains SOMEOBJECT plus the linkers rather than a container
which contains a pointer to SOMEOBJECT plus the linkers.
Im asking because, I am concerned there is an extra level of indirection
within the list that could be avoided by, for example putting the linkers
within SOMEOBJECT and linking SOMEOBJECT's directly to each other via those
linkers.
I know from experience that extra indirection can be a drain on code speed
especially in loops, and a simple case of storing a direct pointer to data
before entering the loop can in places give large speed gains.
e.g. this is slower
for (BYTE i = 0; i < 16; ++i) {
for (BYTE j = 0; j < 16; ++j) {
for (DWORD k = 0; k < 200000; ++k) {
pSomeData->MoreData[i].OtherData[j].EvenMoreData[k].num = 10;
}
}
}
and this is about 25% faster
for (BYTE i = 0; i < 16; ++i) {
MOREDATA *pMoreData = &pSomeData->MoreData[i];
for (BYTE j = 0; j < 16; ++j) {
OTHERDATA *pOtherData = &pMoreData->OtherData[j];
for (DWORD k = 0; k < 200000; ++k) {
pOtherData->EvenMoreData[k].num = 10;
}
}
}
Now you will notice theres more code to avoid extra indirection but with the
extra code comes speed benefits. This is 1 of many reasons why I believe
writing less code isnt always the best thing. Sometimes what seems to be
unproductive, because im writing more code than anyone else, is actually a
benefit, and as speed is important to me, im not always looking for the
solution that saves me cutting and pasting a bit more but the solution that
gives the biggest speed gains.
If you can explain how an STL list is faster or as fast as direct object
linking when using the same algorithms then ill happily convert.
Could you also explain in the following test why the stl code takes over
twice as long to add 100000 objects to the list than my own way.
struct TESTOBJECT
{
BYTE num;
TESTOBJECT *pPrev, *pNext;
TESTOBJECT(){pPrev = pNext = NULL;}
~TESTOBJECT(){pPrev = NULL; if (pNext) delete pNext; pNext = NULL;}
};
stl version - maybe im doing something wrong as i havnt really used it
before, but i cant get it running as fast.
std::list<TESTOBJECT> objectlist;
for (DWORD i = 0; i < 100000; ++i) {
TESTOBJECT Object;
objectlist.push_front(Object);
}
this is over twice as fast and only 3 lines extra of code
TESTOBJECT *pRoot = NULL;
for (DWORD i = 0; i < 100000; ++i) {
TESTOBJECT *pObject = new TESTOBJECT();
pObject->pNext = pRoot;
if (pRoot)
pRoot->pPrev = pObject;
pRoot = pObject;
}
Im still not 100% convinced STL is better for me, but if you can clear these
speed issues up I may be.
Many Thanks.