static void Main(string[] args) { string textFile = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Values.txt"); var watch = new Stopwatch(); string[] lines = File.ReadAllLines(textFile); #region Code for Generation if 1M random Value /* * try * { * using (StreamWriter writer = new StreamWriter(textFile)) * { * for (int i = 1; i <= 100000; i++) * { * Random r = new Random(); * writer.WriteLine(r.Next(10,1000000)); * } * } * } * catch (Exception exp) * { * Console.Write(exp.Message); * }*/ #endregion DynamicIntArray dynamicIntArray = new DynamicIntArray(500000); int[] myArray = new int[100000]; List <int> myList = new List <int>(); ArrayList myArrayList = new ArrayList(); #region DynamicIntArray Analysis Console.WriteLine("Performanace Anaylsis of Custom DynamicIntArray"); watch.Start(); int j = 1; foreach (string line in lines) { Console.WriteLine(j); dynamicIntArray.Add(Convert.ToInt32(line)); j++; } watch.Stop(); Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms"); watch.Restart(); int sum = 0; for (int i = 0; i < dynamicIntArray.curentSize; i++) { sum = sum + dynamicIntArray.Get(i); } watch.Stop(); Console.WriteLine($"Sum of Elements in DynamicIntArray class:- {sum}"); Console.WriteLine($"Execution Time for Traversal: {watch.ElapsedMilliseconds} ms"); watch.Restart(); Console.WriteLine($"Index of 722100:- {dynamicIntArray.indexOf(722100)}"); Console.WriteLine($"Index of 786311:- {dynamicIntArray.indexOf(786311)}"); Console.WriteLine($"Index of 820266:- {dynamicIntArray.indexOf(820266)}"); Console.WriteLine($"Index of 557246:- {dynamicIntArray.indexOf(557246)}"); Console.WriteLine($"Index of 495133:- {dynamicIntArray.indexOf(495133)}"); watch.Stop(); Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms"); #endregion #region C# Array Analysis Console.WriteLine("\n\n\nPerformanace Anaylsis of C# Array Datastructure"); watch.Start(); j = 0; foreach (string line in lines) { if (myArray.Length <= j) { Console.WriteLine("Cant Insert more In Array"); break; } else { myArray[j] = (Convert.ToInt32(line)); j++; } } watch.Stop(); Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms"); watch.Restart(); sum = 0; for (int i = 0; i < myArray.Length; i++) { sum = sum + myArray[i]; } watch.Stop(); Console.WriteLine($"Sum of Elements in Array:- {sum}"); Console.WriteLine($"Execution Time for Traversal: {watch.ElapsedMilliseconds} ms"); watch.Restart(); Console.WriteLine($"Index of 722100:- {Array.FindIndex(myArray, item => item == 722100)}"); Console.WriteLine($"Index of 786311:- {Array.FindIndex(myArray, item => item == 786311)}"); Console.WriteLine($"Index of 820266:- {Array.FindIndex(myArray, item => item == 820266)}"); Console.WriteLine($"Index of 557246:- {Array.FindIndex(myArray, item => item == 557246)}"); Console.WriteLine($"Index of 495133:- {Array.FindIndex(myArray, item => item == 495133)}"); watch.Stop(); Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms"); #endregion #region Generic List<Int> Analysis //Analysis for List<int> generics Console.WriteLine("\n\n\nPerformanace Anaylsis of System.Generics List<> "); watch.Start(); foreach (string line in lines) { myList.Add(Convert.ToInt32(line)); } watch.Stop(); Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms"); watch.Restart(); sum = 0; for (int i = 0; i < myList.Count; i++) { sum = sum + myList[i]; } watch.Stop(); Console.WriteLine($"Sum of elements in List<>:- {sum}"); Console.WriteLine($"Execution Time for Treversal: {watch.ElapsedMilliseconds} ms"); watch.Restart(); Console.WriteLine($"Index of 722100:- {myList.IndexOf(722100)}"); Console.WriteLine($"Index of 786311:- {myList.IndexOf(786311)}"); Console.WriteLine($"Index of 820266:- {myList.IndexOf(820266)}"); Console.WriteLine($"Index of 557246:- {myList.IndexOf(557246)}"); Console.WriteLine($"Index of 495133:- {myList.IndexOf(495133)}"); watch.Stop(); Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms"); #endregion #region System.Collections ArrayList Analysis Console.WriteLine("\n\n\nPerformanace Anaylsis of System.Collection ArrayList"); watch.Start(); foreach (string line in lines) { myArrayList.Add(Convert.ToInt32(line)); } watch.Stop(); Console.WriteLine($"Execution Time for Insertion: {watch.ElapsedMilliseconds} ms"); watch.Restart(); sum = 0; for (int i = 0; i < myArrayList.Count; i++) { sum = sum + (int)myArrayList[i]; } watch.Stop(); Console.WriteLine($"Sum of Elements in ArrayList:- {sum}"); Console.WriteLine($"Execution Time for Treversal: {watch.ElapsedMilliseconds} ms"); watch.Restart(); Console.WriteLine($"Index of 722100:- {myArrayList.IndexOf(722100)}"); Console.WriteLine($"Index of 786311:- {myArrayList.IndexOf(786311)}"); Console.WriteLine($"Index of 820266:- {myArrayList.IndexOf(820266)}"); Console.WriteLine($"Index of 557246:- {myArrayList.IndexOf(557246)}"); Console.WriteLine($"Index of 495133:- {myArrayList.IndexOf(495133)}"); watch.Stop(); Console.WriteLine($"Execution Time for Searching: {watch.ElapsedMilliseconds} ms"); #endregion }
static void Main(string[] args) { int oneMill = 1000000; DynamicIntArray Darr = new DynamicIntArray(oneMill); int [] Carray = new int [oneMill]; ArrayList Aarray = new ArrayList(oneMill); List <int> Larray = new List <int>(oneMill); Random rand = new Random(); for (int i = 0; i < oneMill; i++) { int r = rand.Next(10000); Carray[i] = r; Aarray.Add(r); Larray.Add(r); Darr.Add(r); } // Traversal int sum = 0; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); foreach (int i in Carray) { sum = sum + i; } stopWatch.Stop(); long ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("C# Array : Sum " + sum + " Time " + ts + " MilliSeconds"); // Console.WriteLine(sum + " " + seconds *1000000 + " " ); // Console.WriteLine(date2.Subtract(date1).TotalMilliseconds.ToString()); stopWatch.Reset(); ts = 0; sum = 0; stopWatch.Start(); foreach (int i in Aarray) { sum = sum + i; } stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("ArrayList : Sum " + sum + " Time " + ts + " MilliSeconds"); stopWatch.Reset(); sum = 0; ts = 0; stopWatch.Start(); foreach (int i in Larray) { sum = sum + i; } stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("LIST : Sum " + sum + " Time " + ts + " MilliSeconds"); stopWatch.Reset(); sum = 0; ts = 0; stopWatch.Start(); for (int i = 0; i < oneMill; i++) { sum = sum + Darr.Get(i); } stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("DynamicIntArray : Sum " + sum + " Time " + ts + " MilliSeconds"); int[] fiveRand = new int[5]; for (int i = 0; i < 5; i++) { Console.WriteLine(rand.Next(995082, oneMill)); fiveRand[i] = Darr.Get(rand.Next(999175, oneMill)); // indexes random number between 0 and 1 million adds value at that position to array } for (int i = 0; i < 5; i++) { // For C# Array ts = 0; stopWatch.Reset(); stopWatch.Start(); Array.IndexOf(Carray, fiveRand[i]); stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("Time taken to Search " + fiveRand[i] + " in C# Array : " + ts); // For ArrayList ts = 0; stopWatch.Reset(); stopWatch.Start(); Aarray.IndexOf(fiveRand[i]); stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("Time taken to Search " + fiveRand[i] + " in Linked List : " + ts); // For List ts = 0; stopWatch.Reset(); stopWatch.Start(); Larray.IndexOf(fiveRand[i]); stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("Time taken to Search " + fiveRand[i] + " in Linked List : " + ts); // For List ts = 0; stopWatch.Reset(); stopWatch.Start(); Darr.IndexOf(fiveRand[i]); stopWatch.Stop(); ts = stopWatch.ElapsedMilliseconds; Console.WriteLine("Time taken to Search " + fiveRand[i] + " in Linked List : " + ts); Console.WriteLine("\n\n\n"); } }