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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:10:14+00:00 2026-06-12T22:10:14+00:00

I’m wondering why this code returns different times: My outputs are: Time1: 511 Time2:

  • 0

I’m wondering why this code returns different times:

My outputs are:

Time1: 511 Time2: 228 for N: 100000000
Time1: 509 Time2: 229 for N: 100000001
Time1: 503 Time2: 229 for N: 100000002

I have:

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)
Intel® Pentium(R) CPU B960 @ 2.20GHz × 2 
Linux Ubuntu 12.04

My code looks like:

public class test {

    public static void main(String[] arg)
    {
        for(long N=100000000;N<2000000000;++N){

            long time2 = System.currentTimeMillis();
            double d = 1.0; 
            double z = 1.0/3.0;
            for(long i = 0; i < N; i++)
            {
                d = d*z;
            }
            long result2 = System.currentTimeMillis() - time2;


            long time1 = System.currentTimeMillis();
            double x = 1.0;
            double f = 1.0/3.0;
            for(long i = 0; i < N; i++)
            {
                x = x*f;
            }
            long result1 = System.currentTimeMillis() - time1;



            System.out.println("Time1: " + result1 + " Time2: " + result2 + " bigger: " + (result1 > result2) + " for N: " + N);
        }

    }
}
  • 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-12T22:10:16+00:00Added an answer on June 12, 2026 at 10:10 pm

    Benchmarking code in Java is hard.

    The JVM does lots of things in the background:

    • it optimizes code on the fly,
    • compile methods after a certain number of times they are called (so methods can suddenly get faster, but compilation consumes resources, so everything might get slow for some time),
    • it can move variables outside loops if it sees fit,
    • it can even simply remove some code if it sees that it does nothing (for instance: you are calculating d, but you don’t do anything with it, so why calculate it? the JVM is allowed to simply remove that code; read about “escape analysis”).
    • There might also happen some garbage collection, etc.

    Also, your operating system might interfere with the JVM’s execution, etc.

    So, the bottom line is: without studying a lot on how the JVM and your operating system works, forget about doing precise benchmarks. You can’t expect to get meaningful results.

    For more information, check this question and answers. It contains links to very good papers.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I know there's a lot of other questions out there that deal with this

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.