Array parsing, would you write it like this?

This is a discussion on Array parsing, would you write it like this? within the C++ Programming forums, part of the General Programming Boards category; There is this a quite common question I see, should i do Code: for (int i = 0; i < ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Array parsing, would you write it like this?

    There is this a quite common question I see, should i do
    Code:
    for (int i = 0; i < vec.size(); ++i)
    {
       ...
    }
    or
    Code:
    int len = vec.size();
    for (int i = 0; i < len; ++i)
    {
       ...
    }
    provided of course that the size of vector vec doesn't actually change in the loop and you cannot use the "for each" syntax as you want to have the counter i. I would say that you shouldn't optimize and that there are good chances the compiler will optimize anyway, but in the cases that optimization is a good choice, would you think that it is cleaner to write it instead like:
    Code:
    for (int len = vec.size(), i = 0, ; i < len; ++i)
    {
       ...
    }
    It is a bit more cleaner for me since you typically read line by line when quickly viewing your code so this hides a bit the optimization which is desirable. It is also more to the point as you basically want to write "vec.size()" but you want to instruct the compiler to compute that once.

    I just haven't really see it be coded that way so maybe it is just cleaner to me..

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I wouldn't be surprised if that actually makes it slower, since it makes the code more confusing to the compiler by not following established idiom (which compilers are trained to optimize).

    Since vector::size() is just an accessor for a member, it's a trivial optimization problem for modern compilers (function inlining). The code is so common that someone probably spent a lot of time making sure compilers will generate optimal code for something like this.

    If you really want to do it, I would prefer the first form, because it is an established idiom (for when calculating the number of iterations is actually expensive), so experienced programmers can read it faster.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    Quote Originally Posted by cyberfish View Post
    I wouldn't be surprised if that actually makes it slower, since it makes the code more confusing to the compiler by not following established idiom (which compilers are trained to optimize).
    Compiler writers do tend to assume programmers are inefficient so, if they are going to the trouble of optimising for some idiom, they'll probably put in the effort to detect and optimise variants of it.

    Quote Originally Posted by cyberfish View Post
    Since vector::size() is just an accessor for a member, it's a trivial optimization problem for modern compilers (function inlining).
    There is no requirement that vector::size() accesses a single member. Yes, some library implementations to it that way, but some also do it to a notional equivalent of "return end() - begin();" which actually involves some calculation.

    That said, with most implementations (albeit not all), all member functions of templates (like vector) are inlined, so there is scope for optimising all uses of vectors.

    Quote Originally Posted by cyberfish View Post
    If you really want to do it, I would prefer the first form, because it is an established idiom (for when calculating the number of iterations is actually expensive), so experienced programmers can read it faster.
    Personally, I would use the second or the third.

    Then again, I would probably something different with iterators, and breaking the loop into a separate function
    Code:
         do_stuff(vec.begin(), vec.end());
    where do_stuff() is defined as something like;
    Code:
    template<class Iter> void do_stuff(Iter begin, Iter end)
    {
          while (begin != end)
          {
                // stuff required
    
                ++begin;
          }
    }
    This approach has the advantage that it is not possible to change vec (unless the programmer jumps through certain hoops).

    There are some other options that C++-11 supports, but I'll leave them alone for now.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    For vectors, prefer the first option, or use iterators from begin() to end(). For every implementation of a vector's size() or end() method that I've seen, the difference is going to be pretty much negligible. The cost of it is certainly going to be dwarfed by whatever you have in the loop.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. parsing char array
    By brb9412 in forum C Programming
    Replies: 1
    Last Post: 12-30-2008, 07:20 AM
  3. Parsing char array to CString and int array
    By wawocat in forum C Programming
    Replies: 2
    Last Post: 10-25-2007, 08:05 PM
  4. TCHAR array parsing??
    By dude1978 in forum C++ Programming
    Replies: 6
    Last Post: 05-21-2003, 10:52 AM
  5. Parsing array elements
    By DMaxJ in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2001, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21