Re: Advice on iterator vs. index efficiency

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Neal Becker wrote:
Based on advice from some time back, I write algorithms to use iterators
rather than index-based loops.  Often, this requires jumping through
considerable hoops.

I'm just wondering, with current gcc loop optimizations, it this advice
still relevant?  For example:

< iterator style loop >
for (; i != end (container); ++i)
  do something with (*i);

< index style loop >
for (int i = 0; i < size (container); ++i)
  do something with container[i];

Is there (in general) a difference in speed for these 2 styles?  What about
if the containers are multi-dimensional and loops are nested?

You would have to give a lot more context for anyone to make a judgement. If it were not that the STL uses the first style a lot, and has made it mandatory for compilers to find ways to optimize it, the second version would be much more conducive to optimizations like unrolling and vectorization. I suspect the second style is better for nested loops with current technology. gcc hasn't got very far with optimization of nested loops, so it's up to you to optimize the nesting in source. It's pretty difficult to optimize nested C++ loops anyway.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux