Lets look at below program
public class ExecutionTimeDemo { public static void main(String args[]) { ExecutionTime excTime = new ExecutionTime(); excTime.startTime(); excTime.testMethod(); excTime.executionTime("testMethod execution time :"); } public static void testMethod() { System.out.println("testMethod"); } } class ExecutionTime{ private long startTime; private long endTime; public void startTime() { startTime = System.nanoTime(); } private void stopTime() { endTime = System.nanoTime(); } public void executionTime(String s) { stopTime(); System.out.println(s + (endTime - startTime)); } public void testMethod() { System.out.println("testMethod"); } }
output :
testMethod
testMethod execution time :194788
will display the execution time of testMethod.
In real time, if you want to check whether ArrayList or Vector fast, you can use above program as below
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Vector; public class ExecutionTimeDemo { public static void main(String args[]) { ExecutionTimeDemo excDemo = new ExecutionTimeDemo(); excDemo.arrayListAddAll(); excDemo.vectorAddAll(); } public void arrayListAddAll() { ExecutionTime excTime = new ExecutionTime(); List<string> stringList = Arrays.asList("1","2","3","4"); List<string> arrayList = new ArrayList<string>(); excTime.startTime(); arrayList.addAll(stringList); excTime.executionTime("Array List addAll() = "); } public void vectorAddAll(){ ExecutionTime excTime = new ExecutionTime(); List<string> stringList = Arrays.asList("1","2","3","4"); excTime.startTime(); Vector<string> vector = new Vector<string>(); vector.addAll(stringList); excTime.executionTime("vector addAll() = "); } } class ExecutionTime{ private long startTime; private long endTime; public void startTime() { startTime = System.nanoTime(); } private void stopTime() { endTime = System.nanoTime(); } public void executionTime(String s) { stopTime(); System.out.println(s + (endTime - startTime)); } public void testMethod() { System.out.println("testMethod"); } }
output :
Array List addAll() = 18862
vector addAll() = 31181
so Arraylist is faster than Vector
No comments:
Post a Comment