15

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.

javing
  • 12,307
  • 35
  • 138
  • 211

6 Answers6

15

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);
javing
  • 12,307
  • 35
  • 138
  • 211
  • 2
    @Michael Parker: Try `System.nanoTime();` – Martijn Courteaux Jun 05 '11 at 15:24
  • Beware that this simplistic approach can give misleading answers. – Stephen C Jun 05 '11 at 15:26
  • 1
    Why are you assigning 0 to `endTime`? – Steve Kuo Jun 05 '11 at 15:59
  • 2
    In case he wants to use that code inside a method. You know that local variables don't get automatically initialized as global variables do. No other special reason. – javing Jun 05 '11 at 16:01
  • I wouldn't round down to the second unless this is a very long running test. Usually you only need to run a test for 2-5 seconds to get a fair idea what the timings is like. – Peter Lawrey Jun 05 '11 at 18:44
  • Can't you just do `long endTime = System.currentTimeMillis();` Why bother setting to 0 and then setting it to System.currentTimeMillis()? – Steve Kuo Jun 06 '11 at 02:30
  • 1
    As i said there is no special reason why i did that, also there is no need at all for the variable end time, i could just do: `timeneeded = ((startTime - System.getcurrentMillis())/1000)` but i didn't, no special reason. – javing Jun 06 '11 at 08:47
  • Shouldn't it be: double timeNeeded = ((double)(endTime - startTime ) / 1000); – Peter Dec 30 '12 at 12:14
13

You need to be very careful when writing micro-benchmarks in Java. For instance:

  • If the JIT compiler can figure out that the loop body doesn't affect the results of the code, it can optimize it away. For instance:

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

    is likely to "run" very fast.

  • Code runs a lot faster after it has been JIT compiled.

  • Code can appear to run a lot slower while it is being JIT compiled.

  • If the code allocates objects, then you need to take account of potential variability of measured performance due to the GC running, the initial or maximum heap size being too small and so on.

And of course, performance will depend on your hardware, your operating system, the version and patch level of your JVM, and your JVM launch options.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 3
    +1: When you have a long loop with doesn't do anything, you are actually timing how long it takes to determine that the loop doesn't do anything. ;) – Peter Lawrey Jun 05 '11 at 18:45
4

One way to time an operation is to take an average with nanoTime() You may want to adjust the number of iterations and you will get less variation with an average. nanoTime is better than currentTimeMillis in that it is more accurate and monotonically increasing (it won't go backwards while the application is running)

long start = System.nanoTime();
int runs = 1000*1000;
for(int i=0;i<runs;i++) {
   // do test
}
long time = System.nanoTime() - start;
System.out.printf("The average time taken was %.1f ns%n", (double) time / runs);

Using printf allows you to format the result. You can divide by 1000 to get micro-seconds or 1000000 for micro-seconds.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

This depends on the operation inside the loop so what you should do is record the start time of the loop and end time of the loop and then calculate the difference. You will get the time the loop takes to finish. Example:-

long st = System.currentTimeMillis();

for(int i=0; i < 1000000; i++) {
    // --- loop operation
}

System.out.print("time to execute loop"+
                     ((System.currentTimeMillis() - st) /1000));
Ravinder Payal
  • 2,884
  • 31
  • 40
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
0

divide the number of iteration by 10^8 and you will get the answer in milliseconds .

Saykat
  • 124
  • 2
  • 12
0

This java snippet will be very helpful to you. It is a modified version of above answer

long startTime = System.currentTimeMillis ();
    long startTime_2 = System.nanoTime ();
    long endTime = 0;
    int k = 0;

    for (int i = 0; i < 387420489; i++)
      {
      }

    endTime = System.currentTimeMillis ();
    long endTime_2 = System.nanoTime ();

    long timeneeded = (-1) * (startTime - endTime);
    long timeneeded_2 = (-1) * (startTime_2 - endTime_2);
    System.out.println (timeneeded + " in milliseconds " + timeneeded_2+" time in nanoseconds " );