Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 783857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:37:02+00:00 2026-05-14T20:37:02+00:00

What is the preferred method of writing loops according to efficiency: Way a) /*here

  • 0

What is the preferred method of writing loops according to efficiency:
Way a)

   /*here I'm hoping that compiler will optimize this  
 code and won't be calling size every time it iterates through this loop*/
    for (unsigned i = firstString.size(); i < anotherString.size(), ++i)
    {
    //do something
    }

or maybe should I do it this way:
Way b)

unsigned first = firstString.size();
unsigned second = anotherString.size();

and now I can write:

    for (unsigned i = first; i < second, ++i)
    {
    //do something
    }

the second way seems to me like worse option for two reasons: scope polluting and verbosity but it has the advantage of being sure that size() will be invoked once for each object.
Looking forward to your answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:37:02+00:00Added an answer on May 14, 2026 at 8:37 pm

    I usually write this code as:

    /* i and size are local to the loop */
    for (size_t i = firstString.size(), size = anotherString.size(); i < size; ++i) {
      // do something
    }
    

    This way I do not pollute the parent scope and avoid calling anotherString.size() for each loop iteration.

    It is especially useful with iterators:

    for(some_generic_type<T>::forward_iterator it = container.begin(), end = container.end();
        it != end; ++it) {
      // do something with *it
    }
    

    Since C++ 11 the code can be shortened even more by writing a range-based for loop:

    for(const auto& item : container) {
      // do something with item
    }
    

    or

    for(auto item : container) {
      // do something with item
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code that looks similar to this: public IEnumerable<T> Unfold<T>(this T seed) {
I'm writing a chunk of assembly that will register a tsr and then exit.
What is the preferred method for installing software that requires SQL Server Express? Should
When writing an Android application, what is the preferred method to store constant values,
Lately I've been writing FFI code that returns a data structure in the IO
While finding answer for this query with writing test code, I got to know
What is the preferred method for reading/writing to TCP/IP sockets in PHP? There are
I'm writing an android app that will connect to a REST/JSON webservice. Users will
I am writing a method that should accept as its parameter an object of
What is the preferred method of fetching a url and it's content in app-engine?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.