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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:12:19+00:00 2026-06-01T20:12:19+00:00

Given the code : for (int i = 0; i < n; ++i) {

  • 0

Given the code :

for (int i = 0; i < n; ++i) 
{ 
  A(i) ; 
  B(i) ; 
  C(i) ; 
}

And the optimization version :

for (int i = 0; i < (n - 2); i+=3) 
{ 
  A(i) 
  A(i+1) 
  A(i+2) 
  B(i) 
  B(i+1) 
  B(i+2) 
  C(i) 
  C(i+1) 
  C(i+2)
}

Something is not clear to me : which is better ? I can’t see anything that works any faster using the other version . Am I missing something here ?

All I see is that each instruction is depending on the previous instruction , meaning that
I need to wait that the previous instruction would finish in order to start the one after …

Thanks

  • 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-01T20:12:20+00:00Added an answer on June 1, 2026 at 8:12 pm

    In the high-level view of a language, you’re not going to see the optimization. The speed enhancement comes from what the compiler does with what you have.

    In the first case, it’s something like:

    LOCATION_FLAG;
    DO_SOMETHING;
    TEST FOR LOOP COMPLETION;//Jumps to LOCATION_FLAG if false
    

    In the second it’s something like:

    LOCATION_FLAG;
    DO_SOMETHING;
    DO_SOMETHING;
    DO_SOMETHING;
    TEST FOR LOOP COMPLETION;//Jumps to LOCATION_FLAG if false
    

    You can see in the latter case, the overhead of testing and jumping is only 1 instruction per 3. In the first it’s 1 instruction per 1; so it happens a lot more often.

    Therefore, if you have invariants you can rely on (an array of mod 3, to use your example) then it is more efficient to unwind loops because the underlying assembly is written more directly.

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

Sidebar

Related Questions

Given the following code: int *a = NULL; a = calloc(1, sizeof(*a)); printf(%d\n, a);
Given the following code: int i; ... ostingstream os; os<<i; string s=os.str(); I want
Given the following code : for (int i=0; i<n; i++) { counter += myArray[i];
In c#, Given the following code: public class Person { public int PersonID {
Given the following C# code: var product = new List<int>(); for (int n1 =
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int
here is a code which i want to write. int opt; po::options_description desc(Allowed options);
Given this code: int x = 20000; int y = 20000; int z =
Given the following code: const int size = 20; char buffer[size]; // From the
My original code is given below and works fine. I wanted to add range

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.