tailieunhanh - Microsoft Visual C# 2010 Step by Step (P14)

620 Part VI Building Professional Solutions with Visual Studio 2010 The outer for loop that iterates through values of the integer variable x is a prime candidate for parallelization . You might also consider the inner loop based on the variable i, but this loop takes more effort to parallelize because of the type of i . (The methods in the Parallel class expect the control variable to be an integer .) Additionally, if you have nested loops such as occur in this code, it is good practice to parallelize the outer loops first and then test to see whether the performance of. | 620 Part VI Building Professional Solutions with Visual Studio 2010 The outer for loop that iterates through values of the integer variable x is a prime candidate for parallelization. You might also consider the inner loop based on the variable i but this loop takes more effort to parallelize because of the type of i. The methods in the Parallel class expect the control variable to be an integer. Additionally if you have nested loops such as occur in this code it is good practice to parallelize the outer loops first and then test to see whether the performance of the application is sufficient. If it is not work your way through nested loops and parallelize them working from outer to inner loops testing the performance after modifying each one. You will find that in many cases parallelizing outer loops has the most effect on performance while the effects of modifying inner loops becomes more marginal. 5. Move the code in the body of the for loop and create a new private void method called calculateData with this code. The calculateData method should take an integer parameter called x and a byte array called data. Also move the statements that declare the local variables a b and c from the generateGraphData method to the start of the calculateData method. The following code shows the generateGraphData method with this code removed and the calculateData method do not try and compile this code yet private void generateGraphData byte data for int x 0 x a x private void calculateData int x byte data int a pixelWidth 2 int b a a int c pixelHeight 2 int s x x double p b - s for double i -p i p i 3 double r s i i a double q r - 1 24 r double y i 3 q c plotXY data int -x pixelWidth 2 int y pixelHeight 2 plotXY data int x pixelWidth 2 int y pixelHeight 2 6. In the generateGraphData method change the for loop to a statement that calls the static method as shown in bold here private void generateGraphData byte data 0 .