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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:43:11+00:00 2026-06-10T07:43:11+00:00

This question is identical to this Two loop bodies or one (result identical) but

  • 0

This question is identical to this
Two loop bodies or one (result identical)
but in my case, I use Java.

I have two loops that runs a billion times.

int a = 188, b = 144, aMax = 0, bMax = 0;

for (int i = 0; i < 1000000000; i++) {
  int t = a ^ i;
  if (t > aMax) 
    aMax = t;     
}  

for (int i = 0; i < 1000000000; i++) {
  int t = b ^ i;
  if (t > bMax) 
    bMax = t;     
}  

The time it takes to run these two loops in my machine is appr 4 secs. When I fuse these two loops into a single loop and perform all the operations in that single loop, then it runs in 2 secs. As you can see trivial operations makes up the loop contents, thus requiring constant time.

My question is where am I getting this performance improvement?

I am guessing that the only possible place where performance gets affected in the two separate loops is that it increments i and checks if i < 1000000000 2 billion times vs only 1 billion times if I fuse the loops together. Is anything else going on in there?

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-10T07:43:13+00:00Added an answer on June 10, 2026 at 7:43 am

    If you don’t run a warm-up phase, it is possible that the first loop gets optimised and compiled but not the second one, whereas when you merge them the whole merged loop gets compiled. Also, using the server option and your code, most gets optimised away as you don’t use the results.

    I have run the test below, putting each loop as well as the merged loop in their own method and warmimg-up the JVM to make sure everything gets compiled.

    Results (JVM options: -server -XX:+PrintCompilation):

    • loop 1 = 500ms
    • loop 2 = 900 ms
    • merged loop = 1,300 ms

    So the merged loop is slightly faster, but not that much.

    public static void main(String[] args) throws InterruptedException {
    
        for (int i = 0; i < 3; i++) {
            loop1();
            loop2();
            loopBoth();
        }
    
        long start = System.nanoTime();
    
        loop1();
    
        long end = System.nanoTime();
        System.out.println((end - start) / 1000000);
    
        start = System.nanoTime();
        loop2();
        end = System.nanoTime();
        System.out.println((end - start) / 1000000);
    
        start = System.nanoTime();
        loopBoth();
        end = System.nanoTime();
        System.out.println((end - start) / 1000000);
    }
    
    public static void loop1() {
        int a = 188, aMax = 0;
        for (int i = 0; i < 1000000000; i++) {
            int t = a ^ i;
            if (t > aMax) {
                aMax = t;
            }
        }
        System.out.println(aMax);
    }
    
    public static void loop2() {
        int b = 144, bMax = 0;
        for (int i = 0; i < 1000000000; i++) {
            int t = b ^ i;
            if (t > bMax) {
                bMax = t;
            }
        }
        System.out.println(bMax);
    }
    
    public static void loopBoth() {
        int a = 188, b = 144, aMax = 0, bMax = 0;
    
        for (int i = 0; i < 1000000000; i++) {
            int t = a ^ i;
            if (t > aMax) {
                aMax = t;
            }
            int u = b ^ i;
            if (u > bMax) {
                bMax = u;
            }
        }
        System.out.println(aMax);
        System.out.println(bMax);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two identical strings, one in an array and one in a String
After reading this question here: Passing one Dimension of a Two Dimensional Array in
The following SQL generates all matching records between two tables that have identical schemas
not sure if this question should be here or in serverfault, but it's java-related
I wish I could have a more helpful title for this question, but honestly
I noticed that this answer to a question about .Rprofile settings mentions setting two
I have searched Google and Stackoverflow for this question, but I still don't understand
I have two similar (not identical) dlls, one of them being actually a part
This question is very similar (or almost identical) to In a non blocking socket
This question is similar in concept to this one , except I see I

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.