Skip to content
Advertisement

How much time does it take to execute a loop?

Is there any way to know how many seconds does it take a loop to execute in java?

For example:

for(int i=0; i < 1000000; i++) {

//Do some difficult task goes in here

}

It does not have to be accurate 100%, but its just to have an idea of how long it could take. The algorithm inside is some kind of key generator that writes to a .txt file. I expect it to take even a few mins, so for my first test i want to count the seconds.

Advertisement

Answer

Here you can try this:

long startTime = System.currentTimeMillis();
long endTime = 0;

    for(int i=0; i < 1000000; i++) {

    //Something

    }

endTime = System.currentTimeMillis();

long timeneeded =  ((startTime - endTime) /1000);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement