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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:27:25+00:00 2026-05-29T07:27:25+00:00

I’d like to know which solution could be faster. Let’s say we have mB

  • 0

I’d like to know which solution could be faster. Let’s say we have mB vector of 100 (or n) pointers to objects of B that every each contains a foo() returning a double value:

Code 1:

int main () {
    vector<double> mA;
    for (int i=0; i<100; i++)
        mA.push_back(mB->at(i)->foo());
    for (int i=0; i<100; i++)
        double a = pow((mA.at(i))*(mA.at(i+1)),2);

    return 0;
}

Code 2:

int main () {
     for (int i=0; i<100; i++)
         double a = pow((mB->at(i)->foo()))*(mB->at(i+1)->foo())),2);

     return 0;
}

Code 1 first store the double in a vector, so we have 100 calling to the functions by pointers and then 100+100 accessing to the vector mA. In the second, we have 100+100 calling to the functions by pointes.

1)Is the calling by pointer slower than an access .at() to a vector?
2) which soulution is it better for obtaining a faster code?
Thank you

  • 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-29T07:27:28+00:00Added an answer on May 29, 2026 at 7:27 am

    Just looking at the code, the fact that the first snippet has 2 for loops that go to 100 and the second one has only 1 for loop, the first one would probably be slower.
    The reason? If checks are super slow, and a for loop has a bunch of them. So, the first code snippet has double the amount of if checks. So, I don’t think the difference between both ways of accessing that you’re mentioning will play an important factor.

    What you could try doing is look at the assembly generated by both, or you could use one of the many ways of timing functions. Since you have visual studio, you can try QueryPerformanceCounter:
    http://support.microsoft.com/kb/815668

    Try it and tell us the result!

    PS. I suggested changing the title of your question, since you said “Velocity”, which would be incorrect, since you’re comparing speeds. Probably a better way of asking the question would be “which is faster, accessing… etc etc”, but I didn’t want to change it too much.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I would like to run a str_replace or preg_replace which looks for certain words
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

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.