30

I have the following:

double timeInMinutes = (double) timeInMilliseconds / (1000 * 60);

Is the operation (1000 * 60) done at compile time or at run time? In other words, are there performance differences during run time between the code snippet above and:

double timeInMinutes = (double) timeInMilliseconds / 60000;

EDIT: my question is different from Will the Java compiler precalculate sums of literals?, as I'm mixing the use of variables and literals in the arithmetic operations. It's a small difference, but as @TagirValeev noted in the comments (Are arithmetic operations on literals calculated at compile time or run time?), there are instances where some literals aren't pre-compiled even though they could be.

Community
  • 1
  • 1
neverendingqs
  • 4,006
  • 3
  • 29
  • 57
  • Look up constant expressions. – Sotirios Delimanolis Oct 09 '15 at 14:26
  • 1
    Based on the answers, related: http://stackoverflow.com/questions/2012528/is-there-any-concept-called-constant-folding-in-java – neverendingqs Oct 09 '15 at 14:37
  • This is one of the easiest optimizations that a compiler can implement. Even the old "unoptimized" VB3-VB6 used to do it (not sure about earlier versions, they may have done it too). – RBarryYoung Oct 09 '15 at 18:55
  • 2
    Possible duplicate of [Will the Java compiler precalculate sums of literals?](http://stackoverflow.com/questions/3898259/will-the-java-compiler-precalculate-sums-of-literals) – chue x Oct 09 '15 at 20:32
  • You misinterpret TagirVallev's point: `(double) time / 1000 / 60` isn't an operation on literals, and for floating point reasons, can't even be expected to give the same result as `(double) time / (1000 * 60)`. –  Oct 10 '15 at 00:28

3 Answers3

21

As per JLS §15.2 - Forms of Expressions

Some expressions have a value that can be determined at compile time. These are constant expressions (§15.28).

Multiplicative operators like *, /, and % falls under constant expressions, so it would be determined at compile time.

@SergeyMorozov was faster than me to write and get byte code proof (#2 = Integer 60000) but here is the practical proof and above is theoretical/official statement:

Try generating byte code at your end as well using 1000 * 60 and 60000, and you will see same byte code instructions, and hence there would be same runtime performance.

Java class:

public class Test {
    public static void main(String[] args) {
        int compileTest = 1000 * 60;
    }
}

Byte code:

Classfile /E:/Test.class
  Last modified Oct 9, 2015; size 265 bytes
  MD5 checksum fd115be769ec6ef7995e4c84f7597d67
  Compiled from "Test.java"
public class Test
  SourceFile: "Test.java"
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#13         //  java/lang/Object."<init>":()V
   #2 = Integer            60000
   #3 = Class              #14            //  Test
   #4 = Class              #15            //  java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Utf8               LineNumberTable
   #9 = Utf8               main
  #10 = Utf8               ([Ljava/lang/String;)V
  #11 = Utf8               SourceFile
  #12 = Utf8               Test.java
  #13 = NameAndType        #5:#6          //  "<init>":()V
  #14 = Utf8               Test
  #15 = Utf8               java/lang/Object
{
  public Test();
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=2, args_size=1
         0: ldc           #2                  // int 60000
         2: istore_1
         3: return
      LineNumberTable:
        line 3: 0
        line 4: 3
}
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • Your example `int compileTest = 1000 * 60;` is a bit different than the question; I'm mixing arithmetic with a variable and literals. Reading all the answers, it probably doesn't change the result, but just wanted to note it is slightly different. – neverendingqs Oct 09 '15 at 14:58
  • @neverendingqs I took example to answer your question "*Is the operation (1000 * 60) done at compile time or at run time?*" .. Anyways, key thing is that you can generate byte code and verify at your end apart from the theory .. – hagrawal7777 Oct 09 '15 at 15:02
  • 4
    @hagrawal: I think the OP's question can be better phrased as something more like "will constant expressions still be optimized when they appear as part of a larger, nonconstant expression?" –  Oct 10 '15 at 00:26
  • @Hurkyl When we were answering his question then his question was clear - "*Is the operation (1000 * 60) done at compile time or at run time?*", and that's what we answered. But yes, it can be broadened as you said, and I think OP has edited his question. – hagrawal7777 Oct 10 '15 at 10:52
  • Happy to take any edits to the question title or body. I'm not sure how to accept an answer given the different factors. – neverendingqs Oct 16 '15 at 17:23
10

At compile time. This is one of those most basic compiler optimizations, known as Constant Folding.

sh0rug0ru
  • 1,596
  • 9
  • 9
  • 8
    Probably it worth noting that `timeInMilliseconds / 1000 / 60` would be calculated at runtime. – Tagir Valeev Oct 09 '15 at 14:39
  • @TagirValeev probably worth adding your own answer or editing one of the existing ones to point that out. – neverendingqs Oct 09 '15 at 14:59
  • @TagirValeev decided to try out what you said and it looks like it is calculated at runtime: http://pastebin.com/D2RF0ygF. `timeInMilliseconds / (1000 / 60)` was okay though. – neverendingqs Oct 09 '15 at 16:30
8

Just create class Test

public class Test {
    public static void main(String [] args) {
        long timeInMilliseconds = System.currentTimeMillis();
        double timeInMinutes = (double) timeInMilliseconds / (1000 * 60);
        System.out.println(timeInMinutes);
    }
}

and decompile it using command: javap -v Test

You can see output of decompiled class:

  public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=4, locals=5, args_size=1
     0: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     3: lstore_1
     4: lload_1
     5: l2d
     6: ldc2_w        #3                  // double 60000.0d
     9: ddiv
    10: dstore_3
    11: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
    14: dload_3
    15: invokevirtual #6                  // Method java/io/PrintStream.println:(D)V
    18: return
  LineNumberTable:
    line 3: 0
    line 4: 4
    line 5: 11
    line 6: 18

Take a look on the line 6: ldc2_w #3 // double 60000.0d

Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39