import java.text.DecimalFormat; import java.util.Random; public class AverageSpeedOfStructures { public static void main(String args[]) { //create a ArrayClass object ArrayClass array = new ArrayClass(1000000); //create a SingleLinkedList class object SingleLinkedList<Integer> sll = new SingleLinkedList<Integer>(); //create a Random object Random rand = new Random(); //create a DecimalFormat object DecimalFormat form = new DecimalFormat("##.####"); //declare the required variables to calculate and store double totalofArray = 0; double totalofSLL = 0; long arrInsert, arrDelete, arrSearch; long sllInsert, sllDelete, sllSearch; long start, end; //logic to find the time taken by insert method start = System.nanoTime(); for (int i = 0; i < 1000000; i++) { array.insert(rand.nextInt(1000)); } end = System.nanoTime(); arrInsert = Math.abs(start - end); //logic to find the time taken by search method start = System.nanoTime(); array.search(300); end = System.nanoTime(); arrSearch = Math.abs(start - end); //logic to find the time taken by delete method start = System.nanoTime(); array.delete(300); end = System.nanoTime(); arrDelete = Math.abs(start - end); //average time taken by the ArrayClass using arrays totalofArray = (arrInsert + arrDelete + arrSearch) / 3.0; // Single LinkedList speeds //logic to find the time taken by insert method start = System.nanoTime(); for (int i = 0; i < 1000000; i++) { sll.listInsert(rand.nextInt(1000)); } end = System.nanoTime(); sllInsert = Math.abs(start - end); //logic to find the time taken by search method start = System.nanoTime(); sll.listSearch(300); end = System.nanoTime(); sllSearch = Math.abs(start - end); //logic to find the time taken by delete method start = System.nanoTime(); sll.listRemove(300); end = System.nanoTime(); sllDelete = Math.abs(start - end); //average time taken by the singly linked list totalofSLL = (sllInsert + sllDelete + sllSearch) / 3.0; //print the ratio of speeds System.out .println("The ratio of averge speed of an Unsorted-Optimized" + " array structure \nto the average speed of a" + " Singly Linked List structure is: \n" + form.format(totalofArray) + " : " + form.format(totalofSLL)); } }
ArrayClass
SingleLinkedList<Integer>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)