Skip to content

Instantly share code, notes, and snippets.

@PocasPedro
Last active November 13, 2017 15:55
Show Gist options
  • Save PocasPedro/ca59f8bcf0a3d5ba3929e2d5220d5c5a to your computer and use it in GitHub Desktop.
Save PocasPedro/ca59f8bcf0a3d5ba3929e2d5220d5c5a to your computer and use it in GitHub Desktop.
Java Fundamentals for Kids - Exercise 25
import java.util.concurrent.TimeUnit;
import java.util.Date;
import java.lang.Math;
/**
Program that runs a method for 3 seconds and calculates the time spent doing that execution
*/
public class ExecutionTime {
public static void main(String[] args) {
Date initialDate = new Date();
doSomething();
Date laterDate = new Date();
long timespent;
timespent = Math.abs(laterDate.getTime()-initialDate.getTime())/1000;
System.out.println("The time consumed by this method was : " + timespent + " seconds!");
}
public static void doSomething() {
System.out.println("Doing something ...");
try{
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
System.out.println("Some Error");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment