Example #1
0
        private static void Main()
        {
            var timer = new Stopwatch();

            timer.Start();
            GenerateRandomInputToFile(@"random.txt", 500, 1, 20);
            var boxList = new List<Rect>(ReadBoxFile(@"random.txt"));
            timer.Stop();

            Console.WriteLine("Reading file: {0}", timer.GetTimeString());

            timer.Restart();
            List<MaxRectsBinPack> results = GetResults(boxList).ToList();
            timer.Stop();

            results.Sort(new GridComparer());
            var best = results.First();

            Console.WriteLine("Heuristics: {0}", timer.GetTimeString());
            Console.WriteLine(best.ShortString());

            GeneratePicture(@"random.png", best);
            WriteOutputFile(@"result.txt", best);
            Console.Read();
        }
Example #2
0
 public static void LogExecutionTime(string functionName, Stopwatch sWatch)
 {
     //if (!File.Exists(ExecutionLogFile))
     //{
     //    var stream = File.Create(ExecutionLogFile);
     //    stream.Close();
     //}
     using (StreamWriter w = File.CreateText(ExecutionLogFile))
     {
         Log(functionName, sWatch.GetTimeString(), w);
     }
 }
Example #3
0
        public static void Main(string[] args)
        {
            #region Test Data Creation

            /*
             * //loop for creating test data
             * int numOfArrays = 100;
             * int[] arr;
             * for (int i = 10; i <= 100; i = i + 10) // array size from 1000 to 10,000 in 500 increments
             * {
             *  arr = new int[i];
             *  Console.WriteLine("array size: " + arr.Length + " to begin writing to file.");
             *  for (int j = 1; j <= numOfArrays; j++) // create 500 unique test cases for each array length
             *  {
             *
             *      for (int m = 0; m < arr.Length; m++) // fill array with numbers equal to number of elements
             *      {
             *          arr[m] = m + 1;
             *      }
             *      shuffleArr(arr);
             *
             *      createTestData(arr);
             *
             *
             *  }
             *  Console.WriteLine("array size: " + arr.Length + " finished writing to file.");
             * }
             */


            #endregion


            #region Test cases

            int[] ascendingArray = { 4, 1, 10, 9, 7, 12, 8, 2, 15 };
            Console.WriteLine(" 15, 2, 8, 12, 7, 9, 10, 1, 4 \n ");
            int median = Median(ascendingArray);
            Console.WriteLine("median = " + median);

            int[] ascendingArray2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine("1,2,3,4,5,6,7,8,9");
            int median2 = Median(ascendingArray2);
            Console.WriteLine("median = " + median2);

            int[] ascendingArray3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Console.WriteLine("1,2,3,4,5,6,7,8,9, 10");
            int median3 = Median(ascendingArray3);
            Console.WriteLine("median = " + median3);

            #endregion Test Cases

            for (var i = 10; i <= 100; i = i + 10)
            {
                Console.WriteLine("Analysing data from array size: " + i);

                var arrList     = new List <int[]>( );  // arrayList to hold all the int[] arrays stored in the .csv file
                var timeList    = new List <string>( ); // list for tracking execution time
                var opCountList = new List <int>( );    // list for tracking operation counts

                var sw = new Stopwatch( );

                readFile(arrList, i); // this only grabs the 1000.csv file you can put this in a loop to grab all of them in one go and do the process on them


                foreach (var item in arrList)
                {
                    //Count Basic Operations
                    opCount = 0;
                    var m = BasicOperationBruteForceMedian(item); //Just need to change Algorithms
                    opCountList.Add(opCount);

                    //Measure Execution Time
                    sw = new Stopwatch( );
                    sw.Start( );
                    int medVal = BruteForceMedian(item); //Just need to change Algorithms
                    sw.Stop( );
                    timeList.Add(sw.GetTimeString( ));


                    Console.WriteLine("Median Value: " + medVal + ", Execution Time: " + sw.GetTimeString( ) + ", OpCount: " + opCount);
                }
                Console.WriteLine("Saving Data collected for array size: " + i);
                writeData(timeList, opCountList, i);
                // pause to view console
            }

            Console.WriteLine("Finished All Data Analysis");

            Console.ReadLine( );
        }
Example #4
0
        public static void Main(string[] args)
        {
            #region Test Data Creation

            /* loop for creating test data
             * for (int i = 1000; i <= 10000; i = i + 500) // array size from 1000 to 10,000 in 500 increments
             * {
             *  arr = new int[i];
             *  Console.WriteLine("array size: " + arr.Length + " to begin writing to file.");
             *  for (int j = 0; j < 500; j++) // create 500 unique test cases for each array length
             *  {
             *
             *      for (int m = 0; m < arr.Length; m++) // fill array with numbers equal to number of elements
             *      {
             *          arr[m] = m + 1;
             *      }
             *      shuffleArr(arr);
             *
             *      createTestData(arr);
             *
             *
             *  }
             *  Console.WriteLine("array size: " + arr.Length + " finished writing to file.");
             * }
             */

            #endregion

            for (var i = 1000; i <= 10000; i = i + 500)
            {
                Console.WriteLine("Analysing data from array size: " + i);

                var arrList     = new List <int[]>();  // arrayList to hold all the int[] arrays stored in the .csv file
                var timeList    = new List <string>(); // list for tracking execution time
                var opCountList = new List <int>();    // list for tracking operation counts

                var sw = new Stopwatch();



                readFile(arrList, i); // this only grabs the 1000.csv file you can put this in a loop to grab all of them in one go and do the process on them


                foreach (var item in arrList)
                {
                    opCount = 0;
                    sw      = new Stopwatch();
                    sw.Start();

                    var medVal = Median(item);

                    sw.Stop();
                    timeList.Add(sw.GetTimeString());
                    opCountList.Add(opCount);

                    Console.WriteLine("Median Value: " + medVal + ", Execution Time: " + sw.GetTimeString() + ", OpCount: " + opCount);
                }
                Console.WriteLine("Saving Data collected for array size: " + i);
                writeData(timeList, opCountList, i);
                // pause to view console
            }

            Console.WriteLine("Finished All Data Analysis");
            Console.ReadLine();
        }
        public void FindWithBinarySearch(int N, int min, int max, int key)
        {
            //An object that has randomly generated sorted array of N in [min, max] range
            int[] array = GeneratedArray.Generate(N, min, max);

            Stopwatch     st1    = Stopwatch.StartNew();
            SearchMethods bs     = new SearchMethods();
            int           res1   = bs.BinarySearch(array, key);
            int           count1 = bs.Iterations;

            st1.Stop();

            Stopwatch     st2    = Stopwatch.StartNew();
            SearchMethods ls     = new SearchMethods();
            int           res2   = ls.LinearSearch(array, key);
            int           count2 = ls.Iterations;

            st2.Stop();

            Stopwatch     st3    = Stopwatch.StartNew();
            SearchMethods ipS    = new SearchMethods();
            int           res3   = ipS.InterpolationSearch(array, key);
            int           count3 = ipS.Iterations;

            st3.Stop();

            //Element [key] was found at position [result] in [time] ms - output to file
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"../../../../out/result.txt", true))
            {
                file.WriteLine(String.Format("Params: Size={0}, Min={1}, Max={2}", N, min, max));
                file.WriteLine(String.Format("Binary: Element {0} was found at position {1} in time {2} in {3} iterations", key, res1, st1.GetTimeString(), count1));
                file.WriteLine(String.Format("Linear: Element {0} was found at position {1} in time {2} in {3} iterations", key, res2, st2.GetTimeString(), count2));
                file.WriteLine(String.Format("Interp: Element {0} was found at position {1} in time {2} in {3} iterations\n", key, res3, st3.GetTimeString(), count3));
            }
        }