Exemple #1
0
        // starts a call to fibonacci and captures start/end times
        TimeData StartFibonacci(int n)
        {
            // create a TimeData object to store start/end times
            var result = new TimeData();

            AppendText($"Calculating Fibonacci({n})");
            result.StartTime = DateTime.Now;
            long fibonacciValue = Fibonacci(n);

            result.EndTime = DateTime.Now;

            AppendText($"Fibonacci({n}) = {fibonacciValue}");
            double minutes =
                (result.EndTime - result.StartTime).TotalMinutes;

            AppendText($"Calculation time = {minutes:F6} minutes\r\n");

            return(result);
        }
        //Starts a call to fibonacci and captures start and end times
        TimeData StartFibonacci(int n)
        {
            //Create a TiemData object to store start and end times.
            var result = new TimeData();

            AppendText($"Calculating Fibonacci: ({n})");
            result.StartTime = DateTime.Now;
            long fibonacciValue = Fibonacci(n); //calling the fibonacci method on the n fibonacci # and storing result on fibnacciValue

            result.EndTime = DateTime.Now;

            AppendText($"Fibonacci: ({n}) = " + fibonacciValue);
            double minutes =
                (result.EndTime - result.StartTime).TotalMinutes;

            AppendText($"Calculation time = {minutes:F6} minutes\r\n");

            return(result);
        }
Exemple #3
0
        } // end method startButton_Click

        // starts a call to fibonacci and captures start/end times
        TimeData StartFibonacci(int n)
        {
            // create a ThreadData object to store start/end times
            TimeData result = new TimeData();

            AppendText(String.Format("Calculating Fibonacci({0})", n));
            result.StartTime = DateTime.Now; // time before calculation
            long fibonacciValue = Fibonacci(n);

            result.EndTime = DateTime.Now; // time after calculation

            AppendText(String.Format("Fibonacci({0}) = {1}",
                                     n, fibonacciValue));
            AppendText(String.Format(
                           "Calculation time = {0:F6} minutes\r\n",
                           result.EndTime.Subtract(
                               result.StartTime).TotalMilliseconds / 60000.0));

            return(result);
        } // end method StartFibonacci
Exemple #4
0
        // start asynchronous calls to Fibonacci
        private async void startButton_Click(object sender, EventArgs e)
        {
            try
            {
                int fib        = int.Parse(txtInput.Text); //converts user input into an int
                int fibPlusOne = fib + 1;                  //fibonicci (x + 1)
                int fibPlusTwo = fib + 2;                  // fibonicci (x + 2)


                //validate input is between 1-100
                if (fib < 1 || fib > 100)
                {
                    MessageBox.Show("Number must be between 1-100!");
                    txtInput.Clear();
                }
                else
                {
                    outputTextBox.Text =
                        $"Starting Task to calculate Fibonacci({fib})\r\n";

                    // create Task to perform Fibonacci(user input) calculation in a thread
                    Task <TimeData> task1 = Task.Run(() => StartFibonacci(fib));

                    outputTextBox.AppendText(
                        $"Starting Task to calculate Fibonacci({fibPlusOne})\r\n");

                    // create Task to perform Fibonacci(input plus 1) calculation in a thread
                    Task <TimeData> task2 = Task.Run(() => StartFibonacci(fibPlusOne));

                    outputTextBox.AppendText(
                        $"Starting Task to calculate Fibonacci({fibPlusTwo})\r\n");

                    // create Task to perform Fibonacci(input plus 1) calculation in a thread
                    Task <TimeData> task3 = Task.Run(() => StartFibonacci(fibPlusTwo));

                    var tasks = new Task[]
                    {
                        task1, task2, task3
                    };

                    await Task.WhenAll(tasks); // wait for all to complete

                    // determine time that first thread started
                    DateTime startTime =
                        (task1.Result.StartTime < task2.Result.StartTime && task2.Result.StartTime < task3.Result.StartTime) ?
                        task1.Result.StartTime : task3.Result.StartTime;

                    // determine time that last thread ended
                    DateTime endTime =
                        (task1.Result.EndTime > task2.Result.EndTime && task2.Result.EndTime > task3.Result.EndTime) ?
                        task1.Result.EndTime : task3.Result.EndTime;

                    // display total time for calculations
                    double totalMinutes = (endTime - startTime).TotalMinutes;
                    outputTextBox.AppendText(
                        $"Total calculation time = {totalMinutes:F6} minutes\r\n");
                }
            }
            catch (FormatException) //exception handling if the user enters letters
            {
                MessageBox.Show("Numbers only please!");
                txtInput.Clear();
            }
            // starts a call to fibonacci and captures start/end times
            TimeData StartFibonacci(int n)
            {
                // create a TimeData object to store start/end times
                var result = new TimeData();

                AppendText($"Calculating Fibonacci({n})");
                result.StartTime = DateTime.Now;
                long fibonacciValue = Fibonacci(n);

                result.EndTime = DateTime.Now;

                double minutes =
                    (result.EndTime - result.StartTime).TotalMinutes;

                AppendText($"Fibonacci{n}) = {fibonacciValue} \r\n Calculation time = {minutes:F6} minutes\r\n");

                return(result);
            }
        }