23.4 Sequential Execution of Two Compute-Intensive Tasks

Figure 23.2 uses the recursive Fibonacci method that we introduced in Section 23.3. The example sequentially performs the calculations Fibonacci(46) (line 22) and Fibonacci(45) (line 35) when the user clicks the Start Sequential Fibonacci Calls Button. Note that once you click the Button, the app becomes nonresponsive. This occurs because the Fibonacci calculations are performed in the GUI thread—once the calculations complete, you’ll be able to interact with the app again. Before and after each Fibonacci call, we capture the time (as a DateTime; Section 15.4) so that we can calculate the total time required for that calculation and the total time required for both calculations. In this app, we used DateTime’s overloaded minus (-) operator to calculate the differences between DateTimes (lines 27, 40 and 45)—like method Subtract introduced previously, the minus (-) operator returns a TimeSpan.

The first two outputs show the results of executing the app on a dual-core Windows 10 computer. The last two outputs show the results of executing the app on a single-core Windows 10 computer. In all cases, the cores operated at the same speed. The app always took longer to execute (in our testing) on the single-core computer, because the processor was being shared between this app and all the others that happened to be executing on the computer at the same time. On the dual-core system, one of the cores could have been handling the “other stuff” executing on the computer, reducing the demand on the core performing the synchronous calculation. Results may vary across systems based on processor speeds, the number of cores, apps currently executing and the chores the operating system is performing.

Fig. 23.2 Fibonacci calculations performed sequentially.

Alternate View

   1  // Fig.  23.2: SynchronousTestForm.cs
   2  // Fibonacci calculations performed sequentially
   3  using System;
   4  using System.Windows.Forms;
   5
   6  namespace FibonacciSynchronous
   7  {
   8     public partial class SynchronousTestForm : Form
   9     {
   10       public SynchronousTestForm()
  11       {
  12          InitializeComponent();
  13       } 
  14
  15       // start sequential calls to Fibonacci
  16       private void startButton_Click(object sender, EventArgs e)
  17       {
  18         // calculate Fibonacci (46)
  19         outputTextBox.Text = "Calculating Fibonacci(46)
";
  20         outputTextBox.Refresh(); // force outputTextBox to repaint
  21         DateTime startTime1 = DateTime.Now; // time before calculation
  22         long result1 = Fibonacci(46); // synchronous call
  23         DateTime endTime1 = DateTime.Now; // time after calculation
  24
  25         // display results for Fibonacci(46) 
  26         outputTextBox.AppendText($"Fibonacci(46) = {result1}
");
  27         double minutes = (endTime1 - startTime1).TotalMinutes;
  28         outputTextBox.AppendText(
  29            $"Calculation time = {minutes:F6} minutes

");
  30
  31         // calculate Fibonacci (45)
  32         outputTextBox.AppendText("Calculating Fibonacci(45)
");
  33         outputTextBox.Refresh(); // force outputTextBox to repaint
  34         DateTime startTime2 = DateTime.Now;
  35         long result2 = Fibonacci(45); // synchronous call
  36         DateTime endTime2 = DateTime.Now;
  37
  38         // display results for Fibonacci(45) 
  39         outputTextBox.AppendText($"Fibonacci(45) = {result2}
");
  40         minutes = (endTime2 - startTime2).TotalMinutes;
  41         outputTextBox.AppendText(
  42            $"Calculation time = {minutes:F6} minutes

");
  43
  44         // show total calculation time
  45         double totalMinutes = (endTime2 - startTime1).TotalMinutes;
  46         outputTextBox.AppendText(
  47            $"Total calculation time = {totalMinutes:F6} minutes
");
  48       }
  49
  50       // Recursively calculates Fibonacci numbers
 51       public long Fibonacci(long n)
  52       {
  53          if (n == 0 || n == 1)
  54          {
  55             return n;
  56          }
  57          else
  58          {
  59             return Fibonacci(n - 1) + Fibonacci(n - 2);
  60          }
  61        }
  62     }
  63  }

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset