How to calculation code execution time in seconds using Java

1 Answer

0 votes
import java.util.concurrent.TimeUnit;

public class MyClass {
    public static void main(String args[]) {
        long start = System.nanoTime();
        
        for (int i = 0; i < 2000000; i++);
            
        long time = System.nanoTime() - start;
        double timeInSeconds = time/1e9;
        
        System.out.println(timeInSeconds);
        
    }
}

/*
run:
   
0.003324676
  
*/

 



answered Jun 1, 2018 by avibootz

Related questions

1 answer 147 views
147 views asked Sep 26, 2021 by avibootz
2 answers 142 views
1 answer 303 views
3 answers 213 views
2 answers 255 views
2 answers 279 views
...