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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:31:59+00:00 2026-06-09T16:31:59+00:00

An object memory has a method with the signature BinaryPattern const& getPattern(unsigned int index)

  • 0

An object memory has a method with the signature

BinaryPattern const& getPattern(unsigned int index) const;

I’m using this in the following for-loop:

for (unsigned int k = 0; k < memory->size(); k++) {
    const BinaryPattern s = memory->getPattern(k);
    w += s.at(i) * s.at(j);
}

This is very slow. Surprisingly, I found that the following is much faster:

for (unsigned int k = 0; k < memory->size(); k++) {
    w += memory->getPattern(k).at(i) * memory->getPattern(k).at(j);
}

“getPattern()” does not do any computations, it pretty much just returns the pattern that is stored in a vector.

Why is it so much slower when I store the reference in a variable? I initially did this to speed things up, as I expected retrieving the reference twice to be slower.

  • 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-09T16:32:01+00:00Added an answer on June 9, 2026 at 4:32 pm

    It’s because

    const BinaryPattern s = memory->getPattern(k);
    

    makes a copy of the object by calling its copy constructor. Since you don’t want to change it, store a reference instead:

    const BinaryPattern& s = memory->getPattern(k);
    //                 ^
    //                 note this
    

    (Since capturing an rvalue with a const reference extends the rvalue’s lifetime until the reference dies, this even works should the signature of getPattern() ever be changed to return a copy, rather than a reference.)


    In an optimized build (you didn’t compare debug builds, did you?), the compiler might be able to determine that the two calls to the function in

    memory->getPattern(k).at(i) * memory->getPattern(k).at(j)
    

    don’t have any side effects and is thus keeping the reference to the object obtained by the first call and optimizing away the second call — arriving at the code I proposed above.

    Obviously, the cost of copying a BinaryPattern object is not neglectable.

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

Sidebar

Related Questions

Consider an interface that has only one method: Object getValue(String data); Suppose this describes
This is a method inside my Rectangle class. A Rectangle object has an ivar
This is a C# problem. I have a big object in memory at a
I have an object sitting in memory on the application that I'm using, and
I created a class Foo that has the method toArray() that returns an Array<Int>
If one passes an object to a method using the 'Ref' keyword then what
Class Customer has the following method, which returns an IQueryable with the linq query
Let's say you have a Person object and it has a method on it,
I have a method that has a few pointers as parameters. This method can
I have to assume that the following method doesn't leak memory: public final void

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.