Advertisement

STD:LINKED LISTS question

Started by November 21, 2005 04:36 PM
6 comments, last by GameDev.net 19 years ago
Hi there, If i use a linked list i add an instance like this list<CInstance> ins; CInstance c; c.settings = "SOME TEXT"; ins.pushBack(c); Now the proble with that that i have is that the settings for c is set twice, once when i init it and once when the list PUSHES it onto the list/stack.. Is that correct? Consider that the size of C is like 100KB then moving that around twice will waste alot of cpu cycles! Can any1 answer my with this one? Can i perhaps use ins.PushBacK(NULL); CInstance *c = ins.End()--;
----------------------------

http://djoubert.co.uk
Store pointers in the list instead of the objects. Just be sure to clean up after it. You will need to delete the objects if you only store pointers. Alternately you can use a smart pointer so cleanup will be handled automatically.
Advertisement
ins.push_back("SOME TEXT"); (if proper conversions exist)...
OR
ins.push_back(CInstance("SOME TEXT")) (if the correct constructor exists)

The second one looks as if it would have the same problem, but most compilers will optimize this out.

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

C-strings are stored as pointers. The time taken to push one should be minimal. (unless CInstance::settings is of a type other than char*).

Not that you should really use C strings (char*). Check out std::string.

Are you sure the size of CInstance is 100kb? What happens if you make this program:

int main(){    cout << sizeof(CInstance);    return 0;}

That should tell you how big it really is (in bytes).


Of course, JBourrie is right - the compiler can probably optimise it out the copy - particularly if you construct in the argument to push_back.


Suffice to say, dawidjoubert, that your types are all out of whack in your second piece of code. That won't compile. Not to mention it probably wouldn't be faster.

Best rule of thumb - never try to second-guess your compiler or micro-optimise. Learn about algorithm efficiency - choosing the correct algorithms in the first place is where the big speed-improvements are.
Just out of curiosity, does anyone know of any good resources on how to use smart pointers? I have heard a bit about them but have yet to see any resources on how to use them....
Quote: Original post by Andrew Russell
C-strings are stored as pointers. The time taken to push one should be minimal. (unless CInstance::settings is of a type other than char*).

Not that you should really use C strings (char*). Check out std::string.

Are you sure the size of CInstance is 100kb? What happens if you make this program:

int main(){    cout << sizeof(CInstance);    return 0;}

That should tell you how big it really is (in bytes).


Of course, JBourrie is right - the compiler can probably optimise it out the copy - particularly if you construct in the argument to push_back.


Suffice to say, dawidjoubert, that your types are all out of whack in your second piece of code. That won't compile. Not to mention it probably wouldn't be faster.

Best rule of thumb - never try to second-guess your compiler or micro-optimise. Learn about algorithm efficiency - choosing the correct algorithms in the first place is where the big speed-improvements are.


TRUE MY CODE WONT COMPILE.. But u understood it so whats the problem...

Im going to be frank im using linked lists for saving units for an rts game, with a unit being a massive class storing 3d location,size,health,orientation and much much more. The Class will also have functions in it!

Now considering that the class is big what will be faster?

Look at
CUnit p;
list.push_front(p);
CreateNewUnit(p);
or
CUnit p;
CreateNewUnit(p);
list.Push_Front(p);

// Both push the values onto the memory twice, once when i create the instance p and once when i add it to the list. Is it possible to get list.Push_front() to just allocate the memory required instead of wanting to copy something into it to? This way i can use the resulting pointer to push the values onto the instance and save great processing time!!!

Best rule of thumb - never try to second-guess your compiler or micro-optimise. Learn about algorithm efficiency - choosing the correct algorithms in the first place is where the big speed-improvements are.

That may be true how ever i was rather curious as to wether i am right in my assumtion. An Actually it will be a great improvement, when you have an extreme amount of units being called into creation on diffrent computers (Network game) then it is quite important that this does not take any longer than 1/1000ms
----------------------------

http://djoubert.co.uk
Advertisement
Why not write a program that does one of the proposed operations 1000 times, and one that does the other 1000 times, and then check which is faster instead of relying on others doing your homework (if this performance gain is perceived to be that important)?
--There is only one basic human right and that is the right to do as you damn well please, and with that right comes the only human duty; the duty to take the consequences.-- P.J. O'Rourke
Dr. Evil is right. The issue is that std containers store copies of things. So if you store an object, you get a copy of it. It looks like the settings are set twice because your copy constructor or the default copy constructor is being called when the copy is inserted in the list. If you store pointers, then only the pointer gets copied, not the object.

This topic is closed to new replies.

Advertisement