I am learning about Binary Search Trees, and want to compare the performance of two insertion methods(basic concept is same but there is a little difference in implementation).
I stored time(in nanoseconds) before and after calling the methods and printed the difference. This way, the time required for executing the method called first is always more even when the sequence of calling the methods is reversed.
Common Code :-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] terms = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
BST tree = new BST();
Procedure 1 :-
long t1 = System.nanoTime();
Arrays.stream(terms).forEach(tree::insert);
long t2 = System.nanoTime();
tree = new BST();
long t3 = System.nanoTime();
Arrays.stream(terms).forEach(tree::insert1);
long t4 = System.nanoTime();
System.out.println("insert ->" + (t2 - t1) + "\ninsert1->" + (t4 - t3));
Procedure 2 :-
long t1 = System.nanoTime();
Arrays.stream(terms).forEach(tree::insert1);
long t2 = System.nanoTime();
tree = new BST();
long t3 = System.nanoTime();
Arrays.stream(terms).forEach(tree::insert);
long t4 = System.nanoTime();
System.out.println("insert1->" + (t2 - t1) + "\ninsert ->" + (t4 - t3));
Input :- 12 23 34 45 56 67 78 89 90
Output
Procedure 1 :-
insert ->2155125
insert1->431351
Procedure 2 :-
insert1->2608819
insert ->546649
How can I correct this problem?