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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:41:24+00:00 2026-06-15T14:41:24+00:00

Possible Duplicate: Which of these pieces of code is faster in Java? If i

  • 0

Possible Duplicate:
Which of these pieces of code is faster in Java?

If i write a loop as

for (int i=n; i>=0; i--)

And other one as

for (int i=0; i<=n; i++)

In java which one would be faster and why?..Say n=10000

  • 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-15T14:41:26+00:00Added an answer on June 15, 2026 at 2:41 pm

    Never wonder; use Google Caliper to find out. Since there has been quite a bit of discussion around the relative weights of testing against zero vs. upper limit and incrementing vs. decrementing, here’s the Cartesian product of all these cases:

    import java.util.Random;
    
    import com.google.caliper.Runner;
    import com.google.caliper.SimpleBenchmark;
    
    public class Performance extends SimpleBenchmark {
      static final Random rnd = new Random();
    
      public int timeDecrementToZero(int reps) {
        int sum = rnd.nextInt();
        for (int i = 0; i < reps; i++) {
          for (int j = Integer.MAX_VALUE; j >= 0; j--) sum += j;
        }
        return sum;
      }
      public int timeDecrementFromZero(int reps) {
        int sum = rnd.nextInt();
        for (int i = 0; i < reps; i++) {
          for (int j = 0; j > Integer.MIN_VALUE; j--) sum += j;
        }
        return sum;
      }
      public int timeIncrementFromZero(int reps) {
        int sum = rnd.nextInt();
        for (int i = 0; i < reps; i++) {
          for (int j = 0; j < Integer.MAX_VALUE; j++) sum += j;
        }
        return sum;
      }
      public int timeIncrementToZero(int reps) {
        int sum = rnd.nextInt();
        for (int i = 0; i < reps; i++) {
          for (int j = Integer.MIN_VALUE; j < 0; j++) sum += j;
        }
        return sum;
      }
    
      public static void main(String... args) {
        Runner.main(Performance.class, args);
      }
    }
    

    Results:

     0% Scenario{vm=java, trial=0, benchmark=DecrementToZero} 984060500.00 ns; σ=30872487.22 ns @ 10 trials
    25% Scenario{vm=java, trial=0, benchmark=DecrementFromZero} 982646000.00 ns; σ=35524893.00 ns @ 10 trials
    50% Scenario{vm=java, trial=0, benchmark=IncrementFromZero} 1023745500.00 ns; σ=24828496.82 ns @ 10 trials
    75% Scenario{vm=java, trial=0, benchmark=IncrementToZero} 1081112500.00 ns; σ=20160821.13 ns @ 10 trials
    
            benchmark   ms linear runtime
      DecrementToZero  984 ===========================
    DecrementFromZero  983 ===========================
    IncrementFromZero 1024 ============================
      IncrementToZero 1081 ==============================
    

    Apparently, whether the limit is zero or not has less effect than using inc vs. dec.

    Let’s change it just a tiny bit…

    To point out just how tenouous these differences are, here’s virtually the same code, but now it uses longs (I include one method from the first example, to maintain scale):

      public int timeDecrementFromZeroInt(int reps) {
        int sum = rnd.nextInt();
        for (int i = 0; i < reps; i++) {
          for (int j = 0; j > Integer.MIN_VALUE; j--) sum += j;
        }
        return sum;
      }
      public long timeDecrementFromZero(int reps) {
        long sum = rnd.nextLong();
        for (long i = 0; i < reps; i++) {
          for (long j = 0; j > Integer.MIN_VALUE; j--) sum += j;
        }
        return sum;
      }
      public long timeIncrementFromZero(int reps) {
        long sum = rnd.nextLong();
        for (long i = 0; i < reps; i++) {
          for (long j = 0; j < Integer.MAX_VALUE; j++) sum += j;
        }
        return sum;
      }
      public long timeDecrementToZero(int reps) {
        long sum = rnd.nextLong();
        for (long i = 0; i < reps; i++) {
          for (long j = Integer.MAX_VALUE; j >= 0; j--) sum += j;
        }
        return sum;
      }
      public long timeIncrementToZero(int reps) {
        long sum = rnd.nextLong();
        for (long i = 0; i < reps; i++) {
          for (long j = Integer.MIN_VALUE; j < 0; j++) sum += j;
        }
        return sum;
      }
    

    Results:

     0% Scenario{vm=java, trial=0, benchmark=DecrementFromZeroInt} 978513000.00 ns; σ=14861284.82 ns @ 10 trials
    20% Scenario{vm=java, trial=0, benchmark=DecrementFromZero} 2160652000.00 ns; σ=13825686.87 ns @ 3 trials
    40% Scenario{vm=java, trial=0, benchmark=IncrementFromZero} 2153370000.00 ns; σ=6318160.49 ns @ 3 trials
    60% Scenario{vm=java, trial=0, benchmark=DecrementToZero} 4379893000.00 ns; σ=8739917.79 ns @ 3 trials
    80% Scenario{vm=java, trial=0, benchmark=IncrementToZero} 4383569000.00 ns; σ=5798095.89 ns @ 3 trials
    
               benchmark   ms linear runtime
    DecrementFromZeroInt  979 ======
       DecrementFromZero 2161 ==============
       IncrementFromZero 2153 ==============
         DecrementToZero 4380 =============================
         IncrementToZero 4384 ==============================
    

    Main conclusion: never assume anything about performance at such a low level. Write your full code and test it as a whole because there will always be something else you are not taking into account, which completely turns the tables.

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

Sidebar

Related Questions

Possible Duplicate: when to use StringBuilder in java If not which of these pieces
Possible Duplicate: $('<element>') vs $('<element />') in jQuery Which one of these two are
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc I
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. I
Possible Duplicate: Developing an Android smartphone app - on which devices would YOU suggest
Possible Duplicate: for vs foreach vs while which is faster for iterating through arrays
Possible Duplicate: Why are these numbers not equal? The below expression, which evaluates to
Possible Duplicate: Why are these numbers not equal? in Gnu R: which(seq(0, 1600, 0.05)
Possible Duplicate: Understanding pthread_detach The following code is creating a single thread which prints
Possible Duplicate: Operator overloading I have to code a clock program in which 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.