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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:51:30+00:00 2026-05-29T03:51:30+00:00

My friend and I are stumped. In these two blocks of code, why is

  • 0

My friend and I are stumped. In these two blocks of code, why is the first inner loop faster than the second inner loop? Is this some sort of JVM optimization?

public class Test {
   public static void main(String[] args) {
      int[] arr = new int[100000000];
      arr[99999999] = 1;
      long t1, t2, t3;
      for (int ndx = 0; ndx < 10; ndx++) {
         t1 = System.currentTimeMillis();
         for (int i = 0; i < arr.length; i++)
            if (0 < arr[i])
               System.out.print("");

         t2 = System.currentTimeMillis();
         for (int i = 0; i < arr.length; i++)
            if (arr[i] > 0)
               System.out.print("");

         t3 = System.currentTimeMillis();
         System.out.println(t2 - t1 +" "+(t3 - t2));
      }
   }
}

And the results:

me@myhost ~ $ java Test
57 80
154 211
150 209
149 209
150 209
150 209
151 209
150 210
150 210
149 209

Swapped the orderings of inequalities:

public class Test {
   public static void main(String[] args) {
      int[] arr = new int[100000000];
      arr[99999999] = 1;
      long t1, t2, t3;
      for (int ndx = 0; ndx < 10; ndx++) {
         t1 = System.currentTimeMillis();
         for (int i = 0; i < arr.length; i++)
            if (arr[i] > 0)
               System.out.print("");

         t2 = System.currentTimeMillis();
         for (int i = 0; i < arr.length; i++)
            if (0 < arr[i])
               System.out.print("");

         t3 = System.currentTimeMillis();
         System.out.println((t2 - t1) +" "+(t3 - t2));
      }
   }
}

And the results:

me@myhost ~ $ java Test
56 80
155 210
150 209
149 209
151 210
149 209
150 209
149 208
149 209
149 208

Insanity: doing the same thing over and over again and getting different results.

  • 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-29T03:51:31+00:00Added an answer on May 29, 2026 at 3:51 am

    Short answer: To avoid this problem, put the code you are testing in a separate method. Warm up the method by calling it 11,000 times before you time it. These 2 will allow the JIT compiler to swap the method with a compiled version. Run with -server, it’s just better tuned. Use System.nanoTime() to time stuff. With the following code you’ll get consistent measurements.

    public class AlphaTest
    {
    public static void processA(int[] arr)
    {
        for (int i = 0; i < arr.length; i++)
            if (arr[i] > 0)
                System.out.print("");
    }
    
    public static void processB(int[] arr)
    {
        for (int i = 0; i < arr.length; i++)
            if (0 < arr[i])
                System.out.print("");
    }
    
    public static void main(String[] args)
    {
        int[] smallArr = new int[10];
        for (int i = 0; i < smallArr.length; i++)
        {
            smallArr[i] = 1;
        }
        //warmup
        for (int i = 0; i < 11000; i++)
        {
            processA(smallArr);
            processB(smallArr);
        }
    
        int[] arr = new int[100000000];
        arr[99999999] = 1;
        long t1, t2, t3;
        for (int ndx = 0; ndx < 10; ndx++)
        {
            t1 = System.nanoTime();
            processA(arr);
    
            t2 = System.nanoTime();
            processB(arr);
    
            t3 = System.nanoTime();
            System.out.println(((t2 - t1)/1000000L) + " " + ((t3 - t2)/1000000L));
        }
    }
    }
    

    Long Answer:
    This is definitely a problem of “microbenchmarking” as noted by Matt in the comments. Please see Azul Blog. To support this point of view I am getting the following results depending on how I run the program: as -client as -server and with JIT disabled only 2 result lines per setup, the rest are similar.

    java -client -Xms1024m -Xmx1024m Test
    272 262
    263 252
    ...
    java -server -Xms1024m -Xmx1024m Test
    513 173
    483 201
    ...
    java -client -Djava.compiler=NONE -Xms1024m -Xmx1024m AlphaTest
    2062 1929
    2042 2034
    ...
    java -server -Djava.compiler=NONE -Xms1024m -Xmx1024m AlphaTest
    1844 1864
    1843 1931
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A friend show me this sample code to implement HTTP POST in C# and
A friend wrote some code for me, and there was one file with a
A friend asked me this and I was stumped: Is there a way to
/*I got stumped within my code. I think classes will be simpler than structures,
My friend and I were discussing the other day which style of code is
My friend done this below coding for custom control <a href=javascript:__doPostBack('id','msg');>click</a> now i want
So I have two files: File 1 has this method in it: import MyGlobals
Note that these are my first steps in the world of C# so I
My friend's server has some problem with spoolsv service. While he is searching for
A friend raised this on Twitter: @name_removed doesn't understand why binding generators seem to

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.