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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:39:14+00:00 2026-05-20T03:39:14+00:00

Say I have a loop that looks like this: for(int i = 0; i

  • 0

Say I have a loop that looks like this:

for(int i = 0; i < 10000; i++) {
    /* Do something computationally expensive */
    if (i < 200 && !(i%20)) {
        /* Do something else */
    }
}

wherein some trivial task gets stuck behind an if-statement that only runs a handful of times.
I’ve always heard that “if-statements in loops are slow!” So, in the hopes of (marginally) increased performance, I split the loops apart into:

for(int i = 0; i < 200; i++) {
    /* Do something computationally expensive */
    if (!(i%20)) {
        /* Do something else */
    }
}

for(int i = 200; i < 10000; i++) {
    /* Do something computationally expensive */
}

Will gcc (with the appropriate flags, like -O3) automatically break the one loop into two, or does it only unroll to decrease the number of iterations?

  • 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-20T03:39:14+00:00Added an answer on May 20, 2026 at 3:39 am

    Why not just disassemble the program and see for yourself? But here we go. This is the testprogram:

    int main() {
        int sum = 0;
        int i;
        for(i = 0; i < 10000; i++) {
            if (i < 200 && !(i%20)) {
                sum += 0xC0DE;
            }
            sum += 0xCAFE;
        }
        printf("%d\n", sum);
        return 0;
    }
    

    and this is the interesting part of the disassembled code compiled with gcc 4.3.3 and -o3:

    0x08048404 <main+20>:   xor    ebx,ebx
    0x08048406 <main+22>:   push   ecx
    0x08048407 <main+23>:   xor    ecx,ecx
    0x08048409 <main+25>:   sub    esp,0xc
    0x0804840c <main+28>:   lea    esi,[esi+eiz*1+0x0]
    0x08048410 <main+32>:   cmp    ecx,0xc7
    0x08048416 <main+38>:   jg     0x8048436 <main+70>
    0x08048418 <main+40>:   mov    eax,ecx
    0x0804841a <main+42>:   imul   esi
    0x0804841c <main+44>:   mov    eax,ecx
    0x0804841e <main+46>:   sar    eax,0x1f
    0x08048421 <main+49>:   sar    edx,0x3
    0x08048424 <main+52>:   sub    edx,eax
    0x08048426 <main+54>:   lea    edx,[edx+edx*4]
    0x08048429 <main+57>:   shl    edx,0x2
    0x0804842c <main+60>:   cmp    ecx,edx
    0x0804842e <main+62>:   jne    0x8048436 <main+70>
    0x08048430 <main+64>:   add    ebx,0xc0de
    0x08048436 <main+70>:   add    ecx,0x1
    0x08048439 <main+73>:   add    ebx,0xcafe
    0x0804843f <main+79>:   cmp    ecx,0x2710
    0x08048445 <main+85>:   jne    0x8048410 <main+32>
    0x08048447 <main+87>:   mov    DWORD PTR [esp+0x8],ebx
    0x0804844b <main+91>:   mov    DWORD PTR [esp+0x4],0x8048530
    0x08048453 <main+99>:   mov    DWORD PTR [esp],0x1
    0x0804845a <main+106>:  call   0x8048308 <__printf_chk@plt>
    

    So as we see, for this particular example, no it does not. We have only one loop starting at main+32 and ending at main+85. If you’ve got problems reading the assembly code ecx = i; ebx = sum.

    But still your mileage may vary – who knows what heuristics are used for this particular case, so you’ll have to compile the code you’ve got in mind and see how longer/more complicated computations influence the optimizer.

    Though on any modern CPU the branch predictor will do pretty good on such easy code, so you won’t see much performance losses in either case. What’s the performance loss of maybe a handful mispredictions if your computation intense code needs billions of cycles?

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

Sidebar

Related Questions

Say I have a string that looks like this: str = The &yquick &cbrown
Let's say I have an array that looks like this: $array[0] = 'House'; $array[1]
Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
I have a div class that looks something like this: <div class=objects> X </div>
Let's say I have an object MyObject that looks like this: public class MyObject
I have something that looks like this: my $report = new ReportGenerator; #custom object
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
Lets say we have a native library that works with data like this: double
Lets say I have XML that looks like so (and assume I can't alter
I have some JSON that is sent to my webservice that looks something like

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.