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 8115957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:39:14+00:00 2026-06-06T03:39:14+00:00

Sometimes when looking at optimised code, I see that parameters that are used only

  • 0

Sometimes when looking at optimised code, I see that parameters that are used only within the scope of a loop have their declaration moved outside of the loop.

Something like this:

A arr[BIG_NUMBER];

// .... //

for (int i=0; i!=BIG_NUMBER; ++i)
{
    B b = arr[i].getB();
    // ... do some work with b.
}

being turned into this:

A arr[BIG_NUMBER];

// .... //

B b;
for (int i=0; i!=BIG_NUMBER; ++i)
{
    b = arr[i].getB();
    // ... do some work with b.
}

Presumably the reasoning is that we’re saving on continually redeclaring b. But is this a reasonable thing to do? And is the answer different depending on whether B is a primitive type or a class?

I would have thought that whilst the scope limitations of the variables within the for loop may prevent them from being accessed outside the loop, as the loop’s contents exists within the same stack frame, the ‘actual’ declaration only happens once.

(NB, I’ve considered Creating an object in the loop but consider this to be a different question as it is about any costs associated with declaration rather than initialisation.)

EDIT – improved title

  • 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-06-06T03:39:16+00:00Added an answer on June 6, 2026 at 3:39 am

    If it’s a primitive type, the compiler will optimize accordingly.

    If it’s a user-defined class, it depends. What’s more expensive, one extra initialization or BIG_NUMBER destructors?

    Compare these:

    B b;  //one initialization
    for (int i=0; i!=BIG_NUMBER; ++i)
    {
        b = arr[i].getB();  //BIG_NUMBER assignments
    }
    
    for (int i=0; i!=BIG_NUMBER; ++i)
    {
        B b = arr[i].getB();  //BIG_NUMBER initializations 
                              //should be the same as an assignment
    } //BIG_NUMBER objects destroyed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at some c# code from open and closed source project i see that
While looking at online code samples, I have sometimes come across an assignment of
Looking back over my code I find that I occasionaly have written: ResultSet rs
Sometime when looking through code, I see many methods specify an annotation: @SuppressWarnings(unchecked) What
Looking at the code for some big websites (as I sometimes do) I noticed
Sometimes I would like to quickly see the IL representation of my code snippets
i am looking to answer one of those problems that sometimes get give to
Sometimes we have upwards to 4-6 people either RDPed looking at data in SQL
I've been looking at the sample code and sometimes Apple names xib files ClassNameView
Sometimes we have several classes that have some methods with the same signature, but

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.