Thread: accessing vector elements: iterators are faster?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    accessing vector elements: iterators are faster?

    Is it faster to use iterators rather than random-access to access the elements of a vector, and if so is it really worth converting code to use iterators? (the code I'm considering changing is a matrix class)

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I would say no to both questions. Accessing a particular element might be faster using an iterator since you only have to dereference a pointer, compared to adding the offset to the first element of the array then dereferencing. But to increment an iterator might take longer because of the function call involved (mind you ive never looked at the asm code for this).

    Even if using an iterator were faster, it would be by a miniscule amount and I dont see any reason to bother converting your code to gain slight performance.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 01:56 PM
  2. algorithms over vector: offsets or iterators for speed?
    By pheres in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 02:23 AM
  3. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  4. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  5. Writing Iterators...
    By Geolingo in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM