How to calculate execution time in Java

1 Answer

0 votes
public class MyClass {
    public static void m(int num) {
        long sum = 0;
        for (int i = 0; i < num; i++)
            sum += i;
    }
    public static void main(String args[]) {
        long start = System.nanoTime();

        m(1000);
        
        long end = System.nanoTime();

        System.out.println("Execution time: " + (end - start) + " nanoseconds");
    }
}




/*
run:

Execution time: 15118 nanoseconds

*/

 



answered Jan 19, 2022 by avibootz

Related questions

2 answers 197 views
3 answers 230 views
230 views asked Mar 13, 2023 by avibootz
1 answer 211 views
1 answer 146 views
146 views asked Sep 26, 2021 by avibootz
2 answers 142 views
3 answers 213 views
2 answers 158 views
...