Java looping - declaring a class outside / inside a loop

Possible duplicates:
Which cycle has the best performance? Why?
What is optimal?
Java code efficiency with primitive types

when cycling, for example:

for ( int j = 0; j < 1000; j++) {}; and I need to create an instance of 1000 objects, how is it different when I declare an object inside a loop from declaring it outside the loop ??

 for ( int j = 0; j < 1000; j++) {Object obj; obj =}   

vs

Object obj; 
 for ( int j = 0; j < 1000; j++) {obj =}

Obviously, an object is accessible only from the loop area or from the area that surrounds it. But I do not understand the issue of performance, garbage collection, etc.

What is the best practice? Thanks you

+3
source share
3 answers

. , .

, , . .

+4

. .

0

, 2-4 10000 , , , :

int i=0;

:

int i;
i=0;

, , , , , , , :

package initializer;
public final class EfficiencyTests {
private static class Stoper {
    private long initTime;
    private long executionDuration;

    public Stoper() {
    // TODO Auto-generated constructor stub
    }
    private void start() {
        initTime = System.nanoTime();
    }
    private void stop() {
        executionDuration = System.nanoTime() - initTime;
    }
    @Override
    public String toString() {
        return executionDuration + " nanos";
    }
}

private static Stoper stoper = new Stoper();

public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
        theCycleOfAForLoop(100000);
        theCycleOfAForLoopWithACallToSize(100000);
        howLongDoesItTakeToSetValueToAVariable(100000);
        howLongDoesItTakeToDefineAVariable(100000);
        System.out.println("\n");
    }
}
private static void theCycleOfAForLoop(int loops) {
    stoper.start();
    for (int i = 0; i < loops; i++);
    stoper.stop();
    System.out.println("The average duration of 10 cycles of an empty 'for' loop over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);
}
private static void theCycleOfAForLoopWithACallToSize(int loops) {
    ArrayList<Object> objects=new ArrayList<Object>();
    for (int i = 0; i < loops; i++)
        objects.add(new Object());
    stoper.start();
    for (int i = 0; i < objects.size(); i++);
    stoper.stop();
    System.out.println("The average duration of 10 cycles of an empty 'for' loop with call to size over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);

}
private static void howLongDoesItTakeToSetValueToAVariable(int loops) {
    int value = 0;
    stoper.start();
    for (int i = 0; i < loops; i++) {
        value = 2;
    }
    stoper.stop();
    System.out.println("The average duration of 10 cycles of setting a variable to a constant over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);
}
private static void howLongDoesItTakeToDefineAVariable(int loops) {
    stoper.start();
    for (int i = 0; i < loops; i++) {
        int value = 0;
    }
    stoper.stop();
    System.out.println("The average duration of 10 cycles of initializing and setting a variable to a constant over " + loops + " iterations is: " + stoper.executionDuration * 10 / loops);

}

private static void runAForLoopOnAnArrayOfObjects() {
// TODO Auto-generated method stub

}}

you can find out how much time it will take if you reduce the time of another ... (if you understand what I mean)

Hope this saves you some time.

the thing you need to understand is that I tested this to optimize my update cycle of my platform, and it helped. Adam.

0
source

Source: https://habr.com/ru/post/1746936/


All Articles