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

  • Home
  • SEARCH
  • 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 720203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:43:54+00:00 2026-05-14T05:43:54+00:00

I have the following two programs: long startTime = System.currentTimeMillis(); for (int i =

  • 0

I have the following two programs:

long startTime = System.currentTimeMillis();
for (int i = 0; i < N; i++);
long endTime = System.currentTimeMillis();
System.out.println("Elapsed time: " + (endTime - startTime) + " msecs");

and

long startTime = System.currentTimeMillis();
for (long i = 0; i < N; i++);
long endTime = System.currentTimeMillis();
System.out.println("Elapsed time: " + (endTime - startTime) + " msecs");

Note: the only difference is the type of the loop variable (int and long).

When I run this, the first program consistently prints between 0 and 16 msecs, regardless of the value of N. The second takes a lot longer. For N == Integer.MAX_VALUE, it runs in about 1800 msecs on my machine. The run time appears to be more or less linear in N.

So why is this?

I suppose the JIT-compiler optimizes the int loop to death. And for good reason, because obviously it doesn’t do anything. But why doesn’t it do so for the long loop as well?

A colleague thought we might be measuring the JIT compiler doing its work in the long loop, but since the run time seems to be linear in N, this probably isn’t the case.

I’m using JDK 1.6.0 update 17:

C:\>java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01, mixed mode)

I’m on Windows XP Professional x64 Edition, Service Pack 2, with an Intel Core2 Quad CPU at 2.40GHz.


DISCLAIMER

I know that microbenchmarks aren’t useful in production. I also know that System.currentTimeMillis() isn’t as accurate as its name suggests. This is just something I noticed while fooling around, and I was simply curious as to why this happens; nothing more.

  • 1 1 Answer
  • 3 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-14T05:43:54+00:00Added an answer on May 14, 2026 at 5:43 am

    It’s an interesting question, but to be honest I’m not convinced that considering Hotspot’s behaviour here will yield useful information. Any answers you do get are not going to be transferable in a general case (because we’re looking at the optimisations that Hotspot performs in one specific situation), so they’ll help you understand why one no-op is faster than another, but they won’t help you write faster “real” programs.

    It’s also incredibly easy to write very misleading micro benchmarks around this sort of thing – see this IBM DW article for some of the common pitfalls, how to avoid them and some general commentary on what you’re doing.

    So really this is a “no comment” answer, but I think that’s the only valid response. A compile-time-trivial no-op loop doesn’t need to be fast, so the compiler isn’t optimised to be fast in some of these conditions.

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

Sidebar

Related Questions

I have the following two tables Groups Id (int) People Id (int) GroupId (int,
Consider the two following programs: program one int main() { printf( hello\n ); }
I have written the following code as an intermediate to connect two programs. There
I have a little confusion in the following two statements. The below program is
I have following two arrays. I want the difference between these two arrays. That
How to wrap one div around another? I have following two div ids: #course
I have the following two chunks of code, I am not so sure what
I have the following two tables in my mysql database. Table Name: groups id
I have the following two arrays , i am trying to see whether if
I have the following two queries. SELECT account_name,SUM(amount) AS AMOUNT1 FROM entries LEFT JOIN

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.