General optimisation questions!

Started by
27 comments, last by civguy 19 years, 2 months ago
Hello there, I am currently trying to speed up my application and my profiler has identified that some of my code which is running a bit slowly. This section of code is basically looping through an STL vector. Because this code is greatly depended on in this part of the program I looking for any small increase in speed that I can get. So I was wondering which is faster, usfgin the [] operators to loop through an STL vector, or using an iterator? Thanks in advance. Mark Coleman [Edited by - mrmrcoleman on January 26, 2005 2:07:05 PM]
Advertisement
well, why are you looping through the vector? Only looping through a vector doing nothing can be replaced by a noop which is way faster.
Yes I know, but is one way quicker than the other or not? I have already optimised the code in the loop as far as it will go.

Mark Coleman
Don't limit yourself to using the [] operator or a loop with an iterator. You can likely get much more of an increase in speed using a functor with one of the STL algorithms or a callback function. That depends what you are doing in the loop of course but it's definitely worth checking out.
Rob Segal - Software Developerrob@sarcasticcoder.com
Depends. In one specific section in my code I switched around the std::vector iteration from operator [] to iterators and I got an FPS jump from 200 to 220 (not that significant, but pretty cool still). Try it out and see for yourself, shouldn't take too long!
and I'm using raw pointer:

ptr = &vec[0];

for( num=0; num<vec.size(); num++, ptr++ )
{
....
}

In know that it's not STL standar compliant, but in VC it works since Dinkumware has got continuos vector storage
www.tmreality.com
Cheers guys.

I have a whole load of optimisations to do so I will post them as I find them!

Thanks again.

Mark Coleman
Quote:Original post by tomek_zielinski
In know that it's not STL standar compliant, but in VC it works since Dinkumware has got continuos vector storage


As of 2k3, it actually is standards compliant (vector guaranteed to be contiguous) so you get off easy [grin]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Trap
well, why are you looping through the vector? Only looping through a vector doing nothing can be replaced by a noop which is way faster.


I believe you can safely assume that he is doing something. I bet mrmrcoleman is clever enough to get rid of a loop which ultimately does nothing :)

Not sure about how you do the loop but the [] operator should be nearly free (inlined, direct access and so on) so I guess it isn't the problem. Once inlined, iterator access is rather fast too.

Maybe you can test other stl containers. There are quite a few which are often forgotten.

Quote:Original post by Promit
Quote:Original post by tomek_zielinski
In know that it's not STL standar compliant, but in VC it works since Dinkumware has got continuos vector storage


As of 2k3, it actually is standards compliant (vector guaranteed to be contiguous) so you get off easy [grin]


Woot. Since operator [] const returns a const_reference, then this code is okay too? Weird. Sound like an abuse of the stl...
class Foo{  std::vector<Bar> bars;  typedef std::vector<Bar>::size_type sizetype;  int method() const  {    Bar *b = &bars[0];    for (sizetype i=0; i<s; i++) {      doSomethingWith(b++);    }  }};


Anayway, I can't find any reference about this in the 98 standard (Maybe I'm just a looser, but I still can't find it :))

(edit: edited because I modified something. Hence the edit.)

HTH,
Are you profiling a debug build or a release build? The debug build won't optimise away the inline operator[] access to the vector.

Also, don't call size() every iteration.

Or use std::for_each with a functor.

#include <vector>#include <algorithm>class Thing {};//the functorstruct DoWhatsInTheLoop {    void operator()(const Thing& thing) {        //do what's in the loop    }};int main() {    std::vector<Thing> things;    std::for_each(things.begin(), things.end(), DoWhatsInTheLoop());    return 0;}

This topic is closed to new replies.

Advertisement