![]() |
AndroidBerry.com |
In java we can find the execution time of program using built in method calledSystem.currentTimeMillis()
long time = System.currentTimeMillis(); // returns the current time in the long value
import java.text.DecimalFormat; import java.text.NumberFormat; public class ExecutionTimeEx { void search() { boolean flag=false; int list[] = {12,90,11,30,33},search=0,i=0,loc=0; System.out.println("Array elements are "); for(i=0;i<list.length;i++) System.out.println("list["+i+"] = "+list[i]); search = 30; for(i=0;i<list.length;i++) { if(list[i] == search) { flag=true; loc=i; break; } else flag=false; } if(flag) System.out.println(search+" found at location "+loc); else System.out.println(search+" not found in list"); } public static void main(String[] args) { long start = System.currentTimeMillis(); // get start time new ExecutionTimeEx().search(); long end = System.currentTimeMillis(); // get end time NumberFormat formatter = new DecimalFormat("#0.00000 "); System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds"); } }