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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:02:42+00:00 2026-06-17T20:02:42+00:00

I am doing some simple tests with double, like this: startTime = System.currentTimeMillis(); for

  • 0

I am doing some simple tests with double, like this:

startTime = System.currentTimeMillis();

for (int i = 0; i < 100_000_000; i++) 
{
   doubleCalcTest();
}

endTime = System.currentTimeMillis();    
System.out.println("That took " + (endTime - startTime) + " nanoseconds");

.

public static double doubleCalcTest() 
{
    double x = 987.654321;
    double y = 123.456789;

    x = x + y;
    x = x - y;
    x = x * y;
    return x / y;
}

It turns out that the output is 0 milliseconds. It doesn’t make sense to me because if I set the for loop to run only for 100,000 times, the output is 3 milliseconds.
I found int also acting in the same way.

Can anyone give me a hand on this one? Thanks.

I changed my code to call ‘System.nanoTime’ time and passing in a double value incremented by the index of the loop as suggested.

double x = 123.456789
startTime = System.nanoTime();

for (int i = 0; i < 100_000_000; i++) 
{
   x = x + i;
   doubleCalcTest(x);
}

endTime = System.nanoTime();    
System.out.println("That took " + (endTime - startTime) + " nanoseconds");

.

public static double doubleCalcTest(double x) 
{
    double y = 123.456789;

    x = x + y;
    x = x - y;
    x = x * y;
    return x / y;
}

Run 10,000 times took 503,200 nanoseconds

Run 10,000,000 times took 3,421 nanoseconds

  • 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-17T20:02:43+00:00Added an answer on June 17, 2026 at 8:02 pm

    The JIT discards the execution of DoubleCalcTest since it does not have any side-effect (pure calculation) and the result is not used. The loop itself can also be optimized out since there is no effect.

    Try this with JIT off and it will take around 8000 ms:

    java -Xint snippet.Snippet
    

    At the byteocde level, nothing is optimized out.

    javap -c snippet.Snippet
    

    result:

    public class snippet.Snippet {
      public snippet.Snippet();
        Code:
           0: aload_0       
           1: invokespecial #8                  // Method java/lang/Object."<init>":()V
           4: return        
    
      public static void main(java.lang.String[]);
        Code:
           0: invokestatic  #16                 // Method java/lang/System.currentTimeMillis:()J
           3: lstore_1      
           4: iconst_0      
           5: istore_3      
           6: goto          16
           9: invokestatic  #22                 // Method DoubleCalcTest:()D
          12: pop2          
          13: iinc          3, 1
          16: iload_3       
          17: ldc           #26                 // int 100000000
          19: if_icmplt     9
          22: invokestatic  #16                 // Method java/lang/System.currentTimeMillis:()J
          25: lstore_3      
          26: getstatic     #27                 // Field java/lang/System.out:Ljava/io/PrintStream;
          29: new           #31                 // class java/lang/StringBuilder
          32: dup           
          33: ldc           #33                 // String That took 
          35: invokespecial #35                 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
          38: lload_3       
          39: lload_1       
          40: lsub          
          41: invokevirtual #38                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
          44: ldc           #42                 // String  milliseconds
          46: invokevirtual #44                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          49: invokevirtual #47                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
          52: invokevirtual #51                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          55: return        
    
      public static double DoubleCalcTest();
        Code:
           0: ldc2_w        #64                 // double 987.654321d
           3: dstore_0      
           4: ldc2_w        #66                 // double 123.456789d
           7: dstore_2      
           8: dload_0       
           9: dload_2       
          10: dadd          
          11: dstore_0      
          12: dload_0       
          13: dload_2       
          14: dsub          
          15: dstore_0      
          16: dload_0       
          17: dload_2       
          18: dmul          
          19: dstore_0      
          20: dload_0       
          21: dload_2       
          22: ddiv          
          23: dreturn       
    }
    

    If you try to use to result of DoubleCalc() by assigning it to a variable and print it afterward.

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
    
        double res = 0;
        for (int i = 0; i < 100000000; i++) {
            res = DoubleCalcTest();
        }
    
        System.out.println(res);
        long endTime = System.currentTimeMillis();
        System.out.println("That took " + (endTime - startTime) + " milliseconds");
    }
    

    It will take the same time. Why? The JIT seems to be smart enough to understand that the result does not depend on the number of time the iteration is done.

    However, if you change this to:

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
    
        double res = 0;
        for (int i = 0; i < 100000000; i++) {
            res += DoubleCalcTest();
        }
    
        System.out.println(res);
        long endTime = System.currentTimeMillis();
        System.out.println("That took " + (endTime - startTime) + " milliseconds");
    }
    

    The result depends on the number of iteration and the JIT does not optimize further. In this case, it takes about 100 ms. If I change 100000000 for 200000000, it takes twice as much time.

    So the conclusion is that the JIT stops there.

    NOTE:

    For the give C program:

    #include <stdio.h>
    
    int main(int argc, char** argv) {
    
        long x = 0;
        int i;
    
        for(i=0; i<1000000; i++) {
           x+=i;
        }
        printf("%ld", x);
    }
    

    GCC is able to optimized the loop entirely and compute the value of x at compile-time:

    gcc -O2 -S main.c
    

    main.s:

        .file   "main.c"
        .section    .rodata.str1.1,"aMS",@progbits,1
    .LC0:
        .string "%ld"
        .section    .text.startup,"ax",@progbits
        .p2align 4,,15
        .globl  main
        .type   main, @function
    main:
    .LFB11:
        .cfi_startproc
        movabsq $499999500000, %rsi   <---- See, this is the pre-computed result
        movl    $.LC0, %edi
        xorl    %eax, %eax
        jmp printf
        .cfi_endproc
    .LFE11:
        .size   main, .-main
        .ident  "GCC: (GNU) 4.7.2 20121109 (Red Hat 4.7.2-8)"
        .section    .note.GNU-stack,"",@progbits
    

    Pretty cool, heh?

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

Sidebar

Related Questions

I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out
I'm doing simple tests on all files in directory. But from some reason, sometimes,
I'm doing some simple MS unit tests on my standard, nothing special controller. When
I am doing some performance tests and noticed that a LINQ expression like result
I was doing some simple openframeworks (C++ based) tests drawing different shapes and I
I'm doing some simple tests (in preparation for a larger project) to call an
I've been doing some simple shaders and Im encountering an error that happens randomly,
i've been doing some research on interfaces and a simple layman's explanation for what
I am doing some exercises and tried to convert a simple class into a
What am I doing wrong here: class Helo { // main: generate some simple

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.