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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:14:52+00:00 2026-06-02T21:14:52+00:00

If there is a vector called arr which contains a huge number of data,

  • 0

If there is a vector called arr which contains a huge number of data, and I am to print all the values in that vector. I would either use:

int arr_size = arr.size();
for(int i=0; i<arr_size; ++i) {//print the values}

or implement it this way:

for(int i=0; i<arr.size(); ++i) {//print the values}

In my opinion, the first way of implementation will fetch the size of the vector into the cache and hence make the condition faster after the first miss. What about the second implementation? Is it slower? Will the system call the size() method every time when it meet the condition?

Edit: Let’s say it is using C++.

  • 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-02T21:14:55+00:00Added an answer on June 2, 2026 at 9:14 pm

    Generalising for a loop with arbirary body, there is one critical difference between the two variants you have given: what if the size of arr changes during the loop?

    For the second case, if the compiler can assume that it doesn’t change then it can optimize the loop so arr.size() is only called once, and the generated code becomes about the same as for the first case.

    But if the loop body so much as calls an external function (highly likely) then it can’t make this assumption anymore and must check arr.size() every loop iteration.

    Having said that arr.size() will probably work out to a simple structure member access which would be no slower than storing the value in a local variable, so there isn’t much to be gained out of using the first variant anyway. Unless arr is a pointer or reference in which case it’s an indirect and then an access, so the first version would be a litte faster.

    If it’s a particularly commonly run loop and you have to compile without optimisation for some reason, you might want to go with the first case to speed it up.

    But then, the readability of the code is not impaired much at all by that one extra line is it?

    To sum up, I would go with variant 2 unless the loop was an inner loop that had to be tight, in which case I would go with variant 1 just to be sure.

    And of course if elements are added to arr inside the loop and the loop needs to cover those elements then only the second variant will be correct.

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

Sidebar

Related Questions

I would like to create a so-called matching vector consisting of binaries. All numbers
I would like to call C++ functions in python which return uBLAS vector/matrices. There
Is there a convention for naming the private method that I have called _Add
In the following example should I expect that values.size() will be called every time
In a game that I am writing, I use a 2D vector class which
I Know there's a Vector class in java, but it seems to be just
I am populating a table with a vector however there are some empty cells
Is there a way to convert a vector to a pointer to a pointer
Is there a way to extend a vector by making it repeat itself? >v
Is there a way to call send / recv passing in a vector ?

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.