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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:48:59+00:00 2026-05-26T21:48:59+00:00

I came across simple java program with two for loops. The question was whether

  • 0

I came across simple java program with two for loops. The question was whether these for loops will take same time to execute or first will execute faster than second .

Below is programs :

public static void main(String[] args) {

        Long t1 = System.currentTimeMillis();
        for (int i = 999; i > 0; i--) {
            System.out.println(i);
        }
        t1 = System.currentTimeMillis() - t1;
        Long t2 = System.currentTimeMillis();
        for (int j = 0; j < 999; j++) {
            System.out.println(j);
        }
        t2 = System.currentTimeMillis() - t2;

        System.out.println("for loop1 time : " + t1);
        System.out.println("for loop2 time : " + t2);
    }

After executing this I found that first for loop takes more time than second. But after swapping there location the result was same that is which ever for loop written first always takes more time than the other. I was quite surprised with result. Please anybody tell me how above program works.

  • 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-26T21:49:00+00:00Added an answer on May 26, 2026 at 9:49 pm

    The time taken by either loop will be dominated by I/O (i.e. printing to screen), which is highly variable. I don’t think you can learn much from your example.

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

Sidebar

Related Questions

I came across this (really) simple program a while ago. It just outputs the
While coding very simple program for removing blanks, tabs, newlines I came across something
I wrote a very simple encryption program to practice c++ and i came across
Came across a problem whereby I wanted the last time data was imported to
I came across this question on an interview questions thread. Here is the question:
I came across some java code for doing a prefix search on a JList.
I recently came across some Java code that simply put some strings into a
I once came across a validation framework for java, where you wrote one method
While working on a WebSocket server in Java I came across this strange bug.
Recently, I've been exploring the Java space, and I came across the Spring Framework.

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.