Thread: algorithms over vector: offsets or iterators for speed?

  1. #1
    Registered User
    Join Date
    Nov 2006

    algorithms over vector: offsets or iterators for speed?


    If I have
    std::vector<SomeNode> NodeList;
    and a hand written algorithm which does most of the time throw around some kind of references to the nodes inside nodelist (store, delete, compare, sort, move, de-reference the references).

    What is faster: using offset as reference into the vector or better use iterators?

    I'm not sure.
    While using offsets I'm doomed to always have one indirection.
    But are iterators really faster? I know in VS2005 they are of class type (at least in debug mode) to allow a lot of run time checks. What happens in release? are they plain pointers there?

    Thank you!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Basically they're about the same. There a why one might be a smidgen faster than the other on certain processors, but basically there isn't any noticeable difference.

    You should prefer to use iterators though because it more easily allows for a change of container type.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2006
    there should be 2 other points for using iterators
    -they can be made const
    -I can rely on iterator debugging in debug mode

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  3. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 03:19 AM
  4. relative strength of encryption algorithms (blowfish, des, rinjdael...)
    By duck-billed platypus in forum A Brief History of
    Replies: 3
    Last Post: 12-30-2001, 04:20 PM