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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:42:17+00:00 2026-05-16T02:42:17+00:00

If givin some situation that you can do a loop of a certain event

  • 0

If givin some situation that you can do a loop of a certain event or function that needed to be solved using loops, where you can achieve these by any kind of loop. How can we determine the difference between each loops that can be used based on their speed, efficiency, and memory usage? Like for example, you have these loops

for(int i=0;i<10;i++) {
    array[i] = i+1;
}

int i = 0;
while(i<10) {
    array[i] = i+1;
    i++;
}

The above example have the same output, of course you cannot see the difference between them when executed since this is just a small process, what if you are processing an enormous loop that’s eating up your memory? Which loop is better to use? Is there a proper measure when to use what loop?

Edit:

for pakore answer,

From your answer I can safely say that If I reorder my variables where most of the variables that are dependent are far from each other (with other lines inbetween) could have been more efficient. Say for example,

a=0;
b=1;
c=5;
d=1;
for(int i=0; i<10;i++)
{
    a=b*CalculateAge()+d;
    m=c+GetAvarage(a);
    d++;
}

and

a=0;
b=1;
c=5;
d=1;
for(int i=0; i<10;i++)
{
    a=b*CalculateAge()+d;
    d++;
    m=c+GetAvarage(a);
}

The later is more efficient than the first one since in the later I have called an outside method on the first line and the second line is independent from the result of the first line than on the third line.

Since the first example will wait for the result of the first line before executing the second line and on the third, and the second example has already executed the second line while waiting for the result of the first line.

Conclusion:

An optimized loop doesn’t mater what kind of loop you are using. As what pakore explained, and polygenelubricants, the main thing that you can mind in your loop is how your code is written inside. It is the compilers job to optimize your code, it also helps if you optimize your code according to its dependency with each variable as what pakore explain below.

  • 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-16T02:42:18+00:00Added an answer on May 16, 2026 at 2:42 am

    Well, it’s difficult to explain all the logic behind a loop here.

    The compiler will do amazing things for you in order to optimize the loops, so it does not matter if you use while or for because the compiler will translate to assembler anyway.

    In order to have a deeper understanding you should learn some assembler and then how a basic processor works, how it reads the instructions and how it processes them.

    In order to improve pipelining, it’s better to place statements with the same variables far away from each other. This way, while one statement is calculated, the processor can take the next statement if it’s independent from the first one and start calculating it.

    For example:

    a=0;
    b=3;
    c=5;
    m=8;
    i=0;
    while(i<10){
    a=a+b*c;
    b=b*10+a;
    m=m*5;
    i++;
    }
    

    We have a dependency here between a and b and the statements are right next to each other. But we see that m and i are independent from the rest, so we can do:

    a=0;
    b=3;
    c=5;
    m=8;
    i=0;
    while(i<10){
    a=a+b*c;
    m=m*5;
    i++;
    b=b*10+a;
    
    }
    

    So while a is being calculated, we can start calculating m and i. Most of the times the compiler detects this and does it automatically (it’s calle code reordering). Some times for small loops the compiler copy and pastes the code inside the loop as many times as it’s needed, because it’s faster not to have control variables.

    My suggestion is that let the compiler take care about these things and focus on the costs of the algorithms you are using, it’s better to reduce from O(n!) to O(logn) than to do micro-optimizations inside the loops.

    Update according to the question modified

    Well, the dependencies have to be write/write or read/write dependency. If it’s read/read dependency there’s no problem (because the value does not change). Have a look at the [Data Dependency article] (http://en.wikipedia.org/wiki/Data_dependency).


    In your example, there’s no difference between the two codes, m depends on c and b but these two are never written, so the compiler knows their value before getting into the loop. This is a called a read/read dependency, and it’s not a dependency itself.

    If you had written:

    ...
    m=c+GetAvarage(a);
    ...
    

    Then we would have a write/read dependency (we have to write in a and then read from a, so we have to wait until a is calculated) and the optimization you did would be good.

    But once again, the compiler does this for you and many other things. It’s difficult to say that a micro-optimization in the high level code is gonna have a real impact in the assembler code, because maybe the compiler is doing that already for you, or maybe is reordering the code for you, or maybe is doing a thousand other things better than we can think of at first glance.

    But anyway, it’s good just to know how things work under the carpet 🙂

    Update to add some links
    Have a look at these links to have a further understand about what the compiler can do to improve your code performance:

    Loop unwinding

    Dependency Analysis

    Automatic parallelization

    Vectorization

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

Sidebar

Related Questions

I have been given some matlab code compiled using the .net compiler. I can
My friend is trying to create a utility function that is given some Type
I am wondering if anyone can shed some lights on the situation. I am
I have a situation that I'm sure must be fairly common. I have some
have a weird situation. I'm using Glassfish server for my Enterprise application. In that
Given some (English) word that we shall assume is a plural , is it
Given some typical search form, I can't construct this form action when submitting a
Strange situation: I am trying to remove some hard coding from my code. There
I'm having a weird situation that I'm trying to understand. This piece of code
Given some EventEmitter instance in Node.js , is it absolutely guaranteed that all events

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.