Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / STL: Is this correct way to handle Vectors.

Author
Message
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 3rd Feb 2012 03:31 Edited at: 3rd Feb 2012 03:37
Ok I have been taking a stab at understanding the Standard Template Library, particularly vectors. I spent the day tackling this from all directions. So before I move on, thinking I have complete mastery of the subject. I would like you to check it over. I have written this 5 different ways.

This is what I started with.



And this is my final work.



Thank you.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 3rd Feb 2012 09:39
I can't see anything wrong with this.

You may consider that you can access vector elements just as you would in a normal array using []. That way you can save yourself all the iterator stuff (opposite to linked lists).

For example your for-loop could also look like this:



Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 3rd Feb 2012 12:47
These are great for data objects that don't need to be created/destroyed out of order.

The fastest code is the code never written.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 3rd Feb 2012 18:13
Thanks guys,

Quote: "You may consider that you can access vector elements just as you would in a normal array using []"


That’s actually what I did in my forth example. In my final example I wanted to understand the iterators for use with the STL algorithms.

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 3rd Feb 2012 22:41
I use the vector class from STL quite a lot, but for the sake of speed I use a pointer to the structure/class instead of an iterator which is much faster then using STL stuff. Most of the STL is based on internal calls, even down to scores.size()

Hopefully this example will explain converting a vector to ansi c to speed iterating through a vector much faster than STL.



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 4th Feb 2012 23:06
Taking (scores.size) out of the for loop, does make a whole lot of sense. I understand how the pointer would be faster then the iterator, but would it be faster then accessing it like an array as _Pauli_ suggested.

Thanks
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 5th Feb 2012 02:22
When increasing the iterators position it still has to make a function call to the STL library so converting to standard ansi C you get the added benefit of the speed. In the above example it wouldn't be of any use as the dbPrint() function will still take some time to complete but if you are in a loop full of calculations then you don't want to have to call library functions constantly which using STL generally does. Although STL is fast for most things, it's still much quicker to use ansi C in some cases. Array access in ansi C doesn't make those unecessary STL function calls. STL for the most part is extremely fast and useful and I will never stop using it but just to cut down on bottle-necking I sometimes do have to make the temporary switch over.

If you look at the difference using the debugger, STL use has lots of extra code (calls to the library) as compared to the ansi C array access.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 6th Feb 2012 09:48 Edited at: 6th Feb 2012 09:51
Quote: "If you look at the difference using the debugger, STL use has lots of extra code (calls to the library) as compared to the ansi C array access."

A good compiler will optimize STL to the extent that this doesn't matter. The performance impact of using vectors over arrays is negligible. In general you should focus on readability of your code rather than performance because the compiler is so helpful.

Login to post a reply

Server time is: 2024-11-23 00:27:07
Your offset time is: 2024-11-23 00:27:07