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

  • Home
  • SEARCH
  • 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 4122362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:28:17+00:00 2026-05-20T23:28:17+00:00

I have the following cycle: //condition will be set here to true or false

  • 0

I have the following cycle:

//condition will be set here to true or false

for (int i = 0; i < LARGE_NUMBER; i++) {
    if (condition) {
        //do foo
    } else {
        //do bar
    }
}

Assumption: A cycle if faster without a condition than with a condition. (Is this true?)
Question: Will gcc factor out my if, if condition has been set outside the for cycle, and the cycle itself doesn’t touch condition?

If not, I should switch the if and the for, duplicate code, violate DRY, etc.

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

    For those who don’t wish to read a lengthy post, this optimization is called (in LLVM) Loop Unswitch.

    Why not ask a compiler ?

    void foo(char* c);
    
    int main(int argc, char **argv) {
      bool const condition = argc % 2;
    
      for (int i = 0; i != argc; ++i) {
        if (condition) {
          foo(argv[1]);
        } else {
          foo(argv[0]);
        }
      }
      return 0; 
    }
    

    Is transformed into SSA form (via LLVM try out):

    define i32 @main(i32 %argc, i8** nocapture %argv) {
    entry:
      %0 = icmp eq i32 %argc, 0                       ; <i1> [#uses=1]
      br i1 %0, label %bb5, label %bb.nph
    
    bb.nph:                                           ; preds = %entry
      %1 = and i32 %argc, 1                           ; <i32> [#uses=1]
      %toBool = icmp eq i32 %1, 0                     ; <i1> [#uses=1]
      %2 = getelementptr inbounds i8** %argv, i64 1   ; <i8**> [#uses=1]
      br i1 %toBool, label %bb3.us, label %bb3
    
    bb3.us:                                           ; preds = %bb3.us, %bb.nph
      %i.07.us = phi i32 [ %4, %bb3.us ], [ 0, %bb.nph ] ; <i32> [#uses=1]
      %3 = load i8** %argv, align 8                   ; <i8*> [#uses=1]
      tail call void @_Z3fooPc(i8* %3)
      %4 = add nsw i32 %i.07.us, 1                    ; <i32> [#uses=2]
      %exitcond = icmp eq i32 %4, %argc               ; <i1> [#uses=1]
      br i1 %exitcond, label %bb5, label %bb3.us
    
    bb3:                                              ; preds = %bb3, %bb.nph
      %i.07 = phi i32 [ %6, %bb3 ], [ 0, %bb.nph ]    ; <i32> [#uses=1]
      %5 = load i8** %2, align 8                      ; <i8*> [#uses=1]
      tail call void @_Z3fooPc(i8* %5)
      %6 = add nsw i32 %i.07, 1                       ; <i32> [#uses=2]
      %exitcond8 = icmp eq i32 %6, %argc              ; <i1> [#uses=1]
      br i1 %exitcond8, label %bb5, label %bb3
    
    bb5:                                              ; preds = %bb3, %bb3.us, %entry
      ret i32 0
    }
    

    Not too readable perhaps, so let me point out what’s here:

    • entry: check if argc is equal to 0, if it is, go to bb5 (exit) else go to bb.nph
    • bb.nph: compute the value of condition, if it’s true, go to bb3.us else go to bb3
    • bb3.us and bb3: loops for the true and false condition respectively
    • bb5: exit

    A compiler can pretty much transform your code how it wants, as long as the effect is similar to what you asked for. In this case, it has effectively rewritten the code as:

    int main(int argc, char**argv) {
      if (argc != 0)
      {
        int i = 0;
        if (argc % 2) {
          do {
            foo(argv[1]);
            ++i;
          } while (i != argc);
        } else {
          do {
            foo(argv[0]);
            ++i;
          } while (i != argc);
        }
      }
      return 0;
    }
    

    It’s a form of Loop Invariant Optimization, combined here with a first check to avoid computing the condition if the loop is not going to get executed.

    For those of us who would think the first solution is clearer, we’re quite happy to have the compiler do the nitty gritty optimization for us!

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

Sidebar

Related Questions

I have the following route in my bootstrap: Route::set('cycleadmin', '(<lang>/)cycleadmin(/<model>(/<action>(/<id>)))', array( 'lang' => $lang_options,
Suppose I have the following class: public class Foo { private List<Integer> list =
I'm currently using this jQuery Cycle Plugin: http://jquery.malsup.com/cycle/ And I have the following code:
I have following Crystal Report: Here data which is displayed have period 200804 to
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public

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.