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 CallsButton. 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.