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

  • Home
  • SEARCH
  • 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 60807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:13:11+00:00 2026-05-10T18:13:11+00:00

Is it more efficient for a class to access member variables or local variables?

  • 0

Is it more efficient for a class to access member variables or local variables? For example, suppose you have a (callback) method whose sole responsibility is to receive data, perform calculations on it, then pass it off to other classes. Performance-wise, would it make more sense to have a list of member variables that the method populates as it receives data? Or just declare local variables each time the callback method is called?

Assume this method would be called hundreds of times a second…

In case I’m not being clear, here’s some quick examples:

// use local variables class thisClass {     public:         void callback( msg& msg )         {             int varA;             double varB;             std::string varC;             varA = msg.getInt();             varB = msg.getDouble();             varC = msg.getString();              // do a bunch of calculations          }  };  // use member variables class thisClass {     public:         void callback( msg& msg )         {              m_varA = msg.getInt();              m_varB = msg.getDouble();              m_varC = msg.getString();               // do a bunch of calculations         }      private:         int m_varA;         double m_varB;         std::string m_varC;  }; 
  • 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. 2026-05-10T18:13:12+00:00Added an answer on May 10, 2026 at 6:13 pm

    Executive summary: In virtually all scenarios, it doesn’t matter, but there is a slight advantage for local variables.

    Warning: You are micro-optimizing. You will end up spending hours trying to understand code that is supposed to win a nanosecond.

    Warning: In your scenario, performance shouldn’t be the question, but the role of the variables – are they temporary, or state of thisClass?

    Warning: First, second and last rule of optimization: measure!


    First of all, look at the typical assembly generated for x86 (your platform may vary):

    // stack variable: load into eax mov eax, [esp+10]  // member variable: load into eax mov ecx, [adress of object] mov eax, [ecx+4] 

    Once the address of the object is loaded, int a register, the instructions are identical. Loading the object address can usually be paired with an earlier instruction and doesn’t hit execution time.

    But this means the ecx register isn’t available for other optimizations. However, modern CPUs do some intense trickery to make that less of an issue.

    Also, when accessing many objects this may cost you extra. However, this is less than one cycle average, and there are often more opprtunities for pairing instructions.

    Memory locality: here’s a chance for the stack to win big time. Top of stack is virtually always in the L1 cache, so the load takes one cycle. The object is more likely to be pushed back to L2 cache (rule of thumb, 10 cycles) or main memory (100 cycles).

    However, you pay this only for the first access. if all you have is a single access, the 10 or 100 cycles are unnoticable. if you have thousands of accesses, the object data will be in L1 cache, too.

    In summary, the gain is so small that it virtually never makes sense to copy member variables into locals to achieve better performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can simply use (^|[^\\]), which will either match the… May 11, 2026 at 11:58 pm
  • Editorial Team
    Editorial Team added an answer No, it can't be guaranteed that it will be unique,… May 11, 2026 at 11:58 pm
  • Editorial Team
    Editorial Team added an answer Named constructors are PHP 4 convention. And yes, when you… May 11, 2026 at 11:58 pm

Related Questions

I'm working on a RDF file in my application. I loaded it with the
This is a fairly trivial matter, but I'm curious to hear people's opinions on
So I've been brushing up on my Java skills as of late and have
I had an idea I was mulling over with some colleagues. None of us

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.