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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:55:25+00:00 2026-05-26T23:55:25+00:00

I have the following situation: while (node != NULL && has_all_except) { … }

  • 0

I have the following situation:

while (node != NULL && has_all_except)
{
  ...
}

If neither node nor has_all_except are modified in the loop, will gcc optimize the loop to only compute the expression once?

I have studied the Wikipedia article on compiler optimization (http://en.wikipedia.org/wiki/Compiler_optimization) but couldn’t get a definite answer. My guts says it will be optimized.

  • 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-26T23:55:26+00:00Added an answer on May 26, 2026 at 11:55 pm

    The && operator is lazily evaluated.

    If node != NULL is false then has_all_except won’t even be considered. This is a language rule, not an optimisation too.

    Looping unconditionally if neither has been changed is theoretically possible, depending upon where they could have been modified from and how easy it is to spot that. I suspect though that it’s possibly worse for a modern CPU than just applying the tests. (It would introduce more total branches, more code and greater memory requirements – branch prediction on the other hand ought to do well on loop control such as that).

    You could implement this kind of “optimisation” yourself using goto to test this (note: I’m not advocating using goto, but it does portably “simulate” the effect of the proposed optimisation under discussion). I think looking at an example makes the problems I discussed clearer e.g.:

    #include <stdlib.h>
    
    int do_stuff(); // returns true/false if things were changed
    int other_stuff(); // returns true/false if changed
    
    int main() {
      int has_all_except = 1;
      void *node = &has_all_except;
      while (node != NULL && has_all_except) {
      int changed = 0; // flag to watch for changes
      nochanges: // Place to jump to to unconditionally
        changed |= do_stuff();
        changed |= other_stuff();
        if (!changed)
          goto nochanges; // Unconditional jump
      }
    }
    

    The problem with this is that we’ve succeed in introducing an unconditional jump, but the unconditional jump itself is conditionally applied and that condition is no simpler than the first part of the && itself.

    Doing this means:

    1. One extra int as the “flag” to see if anything got chagned
    2. Cooperation from the do_stuff() and other_stuff() – if anything isn’t cooperative then this isn’t possible (and it’s unlikely your compiler can figure this out for you across trhanslation units)
    3. More code, so less space in whatever cache your processor has
    4. More branches, so more possibility for pipeline stalls, prediction and poorer predictions

    If it’s worth doing and safe to do then I’m fairly confident that it would be done by a suitably modern compiler. If it’s not being done then I suspect it’s not worth it (no better performance), or not safe to apply. Either way the compiler more than likely understands the intricacies of optimising loops for your code on your specific platform far better than the vast majority of programmers!

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

Sidebar

Related Questions

I have the following situation bool user_set_flag; getFlagFromUser(&user_set_flag); while(1){ if(user_set_flag){ //do some computation and
I have the following situation: A user will define a certain filter on a
I have the following situation: I have a certain function that runs a loop
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following situation. A main table and many other tables linked together with
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have following situation, String a=<em>crawler</em> <em> Yeahhhhh </em></a></h3><table; System.out.println(a.indexOf(</em>)); It returns the 11
I have following situation: String a = A Web crawler is a computer program
I have the following situation: I built an Access form with a subform (which

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.