Is there any difference in speed between these codes?

int a; for (int i = 0; i < x; i++){ a = sc.nextInt(); } 

and

 for (int i = 0; i < x; i++){ int a = sc.nextInt(); } 

In the cycle, let's say I compare with something. Those. I overwrite the variable each time in the first case, and in the second I create a new one? Does memory somehow affect speed?
PS you need to know to solve Olympiad problems.

  • 2
    When solving Olympiad problems, you must rely on the correct algorithms, and not on strange micro-optimizations. Even if some version of the optimizer makes one of the two semantically identical options faster, a gain of a few nanoseconds will not give you anything. - VladD
  • four
    The normal compiler will see that the result of the method is not used and will completely throw out the loop. - KoVadim
  • If you need an I / O rate, write an Olympiad in C / C ++. In vain, something in Java before TL was 1.5 times more. And I / O is slower at 5 times ... - pavel
  • 2
    All high-level languages ​​have optimizers that optimize code at compile time. You do not even know what is actually happening with your variable until you get into the binary and read it! - Makarenko_I_V
  • one
    The static compiler and, in particular, the Java JIT compiler are now using such aggressive speculative optimizations that it makes no sense to think about such manual "optimizations". Thanks to the most detailed live statistics about your code, the JIT compiler optimizer can easily overtake the C / C ++ optimizer, which only makes assumptions from the source code. - Nofate

3 answers 3

Here is a good example.

Do not take variables out of cycles.

Often there is a situation when the declaration of variables is carried out beyond the boundaries of cycles. Such a situation is fraught with errors, since access to variables is possible outside the area in which their application was intended. This is especially likely for variables with common names, such as i or tmp. In particular, old values ​​can be saved in variables and break the work of the subsequent code in the most unpredictable way.

Students who have an excessive propensity to optimize may argue that this will degrade the performance of the code, since more actions will have to be performed at each iteration of the loop. However, an unnecessary variable declaration does not require the execution of any instructions. Moving the declaration inside the loop does not affect the performance of the application running on the Java platform.

  • Thanks, the most intelligible answer. - Exctues
  • The key word in the answer is "students." - VladD

In this case, it makes sense to see how the code looks in both cases after compilation.

Java code:

 int x = 10; Random sc = new Random(); int a; for (int i = 0; i < x; i++){ a = sc.nextInt(); System.out.println(a); } 

Part of the code byte that executes the instructions a = sc.nextInt();System.out.println(a); :

 LINENUMBER 14 L5 ALOAD 2 INVOKEVIRTUAL java/util/Random.nextInt ()I ISTORE 3 L6 LINENUMBER 15 L6 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ILOAD 3 INVOKEVIRTUAL java/io/PrintStream.println (I)V 

The ISTORE 3 and ILOAD 3 represent working with the local variable а .

Java code:

 int x = 10; Random sc = new Random(); for (int i = 0; i < x; i++){ int a = sc.nextInt(); System.out.println(a); } 

Part of the code byte that executes instructions int a = sc.nextInt(); System.out.println(a); int a = sc.nextInt(); System.out.println(a); :

 LINENUMBER 13 L5 ALOAD 2 INVOKEVIRTUAL java/util/Random.nextInt ()I ISTORE 4 L6 LINENUMBER 14 L6 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ILOAD 4 INVOKEVIRTUAL java/io/PrintStream.println (I)V 

The ISTORE 4 and ILOAD 4 represent working with the local variable а .

About local variables in jvm can be read here . From the above code, it is clear that in both cases we get an identical byte code, which means that the memory is used in the first case as much as in the second and the speed of work will be the same.

    You can measure the speed of code execution yourself and understand which is faster: long s1 = new Date().getLong(); код long s2 = new Date().getLong(); long res=s2-s1;

    • 2
      good luck with that! What will you do if the cost of creating the Date will be more than the measured code itself? How will you measure the error? - etki
    • unless costs will be equal from both dimensions? The point is to see the difference, and not to measure a specific example. And the difference remains the same in any case - BogdanBida