CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Is there any danger in doing this?

  1. #1
    Join Date
    Jan 2009
    Posts
    1,689

    Is there any danger in doing this?

    I just changed job and my new company has a lot of code like this:
    Code:
    for(foo::iterator it = foobar.begin(); it != foobar.end(); ++it)
    I always did it this way:
    Code:
    for(foo::iterator it = foobar.begin(), it_end = foobar.end(); it != it_end; ++it)
    (Indirectly through a foreach macro of course)

    The latter is quiet a bit faster. The only reason I can think of to do the former is if the loop changes what's in the container. Is there something I'm missing?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: Is there any danger in doing this?

    Your method is typically better, but as you say in specific cases it is not appropriate.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Is there any danger in doing this?

    Perhaps they thought the call to the end function would be optimized and so it wouldn't make a difference. Did you look at the assembly code to confirm? Perhaps you should point it out to them if your way results in significant run time improvement. I'm not sure what you mean by "quite a bit faster". You could profile two functions, analyze the difference, and then use the data to convince your new teammates that there is a better way. I looked at the assembly code for a small project and as you point out there is less code executing as the body of the for loop doing it your way. This is one of the reasons for item 43 of "Effective STL" by Scott Meyers.
    Last edited by kempofighter; April 12th, 2010 at 03:36 PM. Reason: Added book reference.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Is there any danger in doing this?

    I didn't look at the assembly, but the loop itself in 3x faster if calling end over and over again is not done. I don't think the compiler is smart enough to optimize that out, as end() is a function and it's not const.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Is there any danger in doing this?

    Quote Originally Posted by ninja9578 View Post
    I didn't look at the assembly, but the loop itself in 3x faster if calling end over and over again is not done. I don't think the compiler is smart enough to optimize that out, as end() is a function and it's not const.
    I still would have expected to be inlined though...

  6. #6
    Join Date
    Oct 1999
    Location
    ks
    Posts
    505

    Re: Is there any danger in doing this?

    it might be quite revealing for you to raise the issue. if your method is just as reliable but typically faster then suggest the standard be changed. no reason to assume that just because it is in place it has been communicated by god. it might give you an inidcation of what type od corporate environment you've signed onto.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)