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

The Archive Base Latest Questions

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

Assuming buffer is a struct composed of several members, including an array. Compiling with

  • 0

Assuming buffer is a struct composed of several members, including an array.

Compiling with usual optimization flags, what kind of performance difference should one expect from running these two code snippets ?

buffer buf;
for (int i = 0; i < BIG_ENOUGH_NUMBER; i++) {
   init(huge_file, i, &buf);
}

// buf is not used afterward

...

void init(FILE* f, int i, buffer* b) {
   ... // b is filled using f, according to i
   do_something(b);
}

vs

for (int i = 0; i < BIG_ENOUGH_NUMBER; i++) {
   init(huge_file, i);
}

...

void init(FILE* f, int i) {
   buffer buf;
   ... // buf is filled from scratch using f, according to i
   do_something(&buf);
}
  • 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-02T16:58:15+00:00Added an answer on June 2, 2026 at 4:58 pm

    The first answer is: benchmark them.

    A second answer is: compile them to assembly language, and look at the source. Doing this with and without optimsation flags could be instructive.

    As pointed out be abelenky, in both cases buf is on the stack. My general guess is that the second case would be slightly faster (with compilers I’ve used), because buf does not have to be passed on as an argument. It does have to be allocated on the stack, but stack allocation is typically just a slightly different size of the function’s call frame. The same amount of work has to be done (adjusting the stack pointer) regardless of how big that frame is.

    So I’d expect the main difference in generated code would be one fewer “PUSH” instructions in the second case, assuming that there are enough arguments that some will have to go on the stack. (If they’re all in registers, that’s a bit different.)

    It may be affected by optimisation, for instance whether buf ends up in a register in each case. But the missing code to populate buf could affect this so I won’t speculate.

    Note that the above is my guess based on looking at the behaviour of my compiler. In theory, compilers can translate code however they like as long as the resulting program runs correctly, which makes it hard to generalise about what optimisations they may or may not do.

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

Sidebar

Related Questions

Assuming MyClass uses the default destructor (or no destructor), and this code: MyClass *buffer
Is it possible to iterate of a C struct, where all members are of
Given (in C++) char * byte_sequence; size_t byte_sequence_length; char * buffer; size_t N; Assuming
Assuming i got 3 data:i=01,j=02,k=03 and i want to set these 3 data indiviually
Assuming I have a defined class class MyClass { } Is there a hit
Assuming this works properly for telling if a substring is in a string, is
Assuming there is a std::deque queue of pointers to tasks to be performed, what
Assuming such a query exists, I would greatly appreciate the help. I'm trying to
Assuming you can't use LINQ for whatever reason, is it a better practice to
Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),

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.