Advertisement
makispaiktis

Counting time of for-loops in Java

Mar 15th, 2020 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. import java.time.Duration;
  2. import java.time.Instant;
  3.  
  4. public class Clock {
  5.    
  6.     // Method
  7.     public static long countTimeOfForLoops(int N){
  8.         long start = System.currentTimeMillis();
  9.         for(int i=0; i<Math.pow(10, N); i++){
  10.             // Some time passes away
  11.         }
  12.         long end = System.currentTimeMillis();
  13.         long elapsedTimeInMillis = end - start;
  14.         return elapsedTimeInMillis;
  15.     }
  16.    
  17.     public static void main(String[] args) {
  18.          for(int i=0; i<10; i++){
  19.              System.out.println("Time for looping " + Math.pow(10, i) + " times: " + (int)countTimeOfForLoops(i));
  20.          }
  21.     }  
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement