Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

How to calculate the running time of a program in Java

4 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        long startTime = System.currentTimeMillis();
        
        // program
        long sum = 0;
        for (int i = 1; i <= 1000_000_000; i++) {
			    sum += 1;
		}	
        // program

        long endTime = System.currentTimeMillis();

        long elapsedTime = endTime - startTime;

        System.out.println("Elapsed time: " + elapsedTime + " milliseconds");
    }
}
 
 
 
 
/*
run:
      
Elapsed time: 15 milliseconds
      
*/

 



answered Oct 6, 2023 by avibootz
edited Oct 6, 2023 by avibootz
0 votes
import java.util.Calendar;
import java.util.Date;

public class MyClass {
     public static int getMonthFromCurrentDate() {
        Date date = new Date();
          
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
  
        return cal.get(Calendar.MONTH) + 1;
    }
    public static void test(long sum) {
        sum += 1;
    }
    public static void main(String args[]) {
        long startTime = System.currentTimeMillis();
        
        // program
        long sum = 0;
        for (int i = 1; i <= 1_000_000; i++) {
			    sum += 1;
			    sum += 2;
			    test(sum);
			    int month = getMonthFromCurrentDate();
		}	
        // program

        long endTime = System.currentTimeMillis();

        long elapsedTime = endTime - startTime;
        
        long elapsedTimeInSeconds = elapsedTime / 1000;
        long elapsedTimeInMinutes = elapsedTime / 60000;

        System.out.println(elapsedTime + " milliseconds");
        System.out.println(elapsedTimeInSeconds + " seconds");
        System.out.println(elapsedTimeInMinutes + " minutes");
    }
}
 
 
 
 
/*
run:
      
1739 milliseconds
1 seconds
0 minutes
      
*/

 



answered Oct 6, 2023 by avibootz
edited Oct 6, 2023 by avibootz
0 votes
import java.util.Calendar;
import java.util.Date;
import java.time.Duration;
import java.time.Instant;

public class MyClass {
     public static int getMonthFromCurrentDate() {
        Date date = new Date();
          
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
  
        return cal.get(Calendar.MONTH) + 1;
    }
    public static void test(long sum) {
        sum += 1;
    }
    public static void main(String args[]) {
        Instant start = Instant.now();
        
        // program
        long sum = 0;
        for (int i = 1; i <= 1_000_000; i++) {
			    sum += 1;
			    sum += 2;
			    test(sum);
			    int month = getMonthFromCurrentDate();
		}	
        // program

        Instant end = Instant.now();
		
		System.out.println(Duration.between(start, end));
    }
}
 
 
 
 
/*
run:
      
PT1.754859235S
      
*/

 



answered Oct 6, 2023 by avibootz
edited Oct 6, 2023 by avibootz
0 votes
import java.util.Calendar;
import java.util.Date;

public class MyClass {
     public static int getMonthFromCurrentDate() {
        Date date = new Date();
          
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
  
        return cal.get(Calendar.MONTH) + 1;
    }
    public static void test(long sum) {
        sum += 1;
    }
    public static void main(String args[]) {
        long startTime = System.nanoTime();
        
        // program
        long sum = 0;
        for (int i = 1; i <= 1_000_000; i++) {
			    sum += 1;
			    sum += 2;
			    test(sum);
			    int month = getMonthFromCurrentDate();
		}
		// program

        long endTime = System.nanoTime();
        long totalTime = endTime - startTime;
        
        System.out.println(totalTime + " nanoseconds");
    }
}
 
 
 
 
/*
run:
      
1761862795 nanoseconds
      
*/

 



answered Oct 6, 2023 by avibootz
...