开发者

How much time does it take to execute a loop?

开发者 https://www.devze.com 2023-03-11 09:18 出处:网络
Is there any way to know how many seconds does it take 开发者_运维百科a loop to execute in java?

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.


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);


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.


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.


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));


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


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 " );
0

精彩评论

暂无评论...
验证码 换一张
取 消