For the following code segment, apply the loop optimization techniques to improve the code quality: 1) code motion, 2) loop unrolling, 3) loop fusion for (i = 0; i < N * 2; i++) a[i] = b[i] * c[i]; for (j = 0; j < N * 2; j++) m[j] = m[j] + 5;
Added by William B.
Step 1
int n2 = N*2; for(i=0; i<n2; i++) a[i] = b[i] * c[i]; for(j=0; j<n2; j++) m[j] = m[j] + 5; Show more…
Show all steps
Your feedback will help us improve your experience
Yujie Wang and 66 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Determine how many times the innermost loop will be iterated when the following algorithm segment is implemented and run. (Assume that m and n are positive integers.) for j := 1 to m for k := 1 to n [Statements in body of inner loop. None contain branching statements that lead outside the loop.] next k next j (Hint: See Example 9.2.5 in the "Read It" link.) For each iteration of the outer loop there are iterations of the inner loop. Therefore, the answer is mn
Yujie W.
(c) We have the following C language code to implement a nested loop: for (i=1; i<=10; i++){ for (j=20; j>5; j--){ ... /*loop body */ } } It was implemented in the following manner (see Figure 3) in ARM assembly language. However, it was found to be inefficient. Use your knowledge of loops in ARM assembly language to optimize the program. (10 marks) MOV r1, #1 ;i=1 OutLoop CMP r1, #10 ;i<10? BGT OutDone ;if j>10, finish outer loop MOV r2, #20 ;j=20 InLoop CMP r2, #5 ;j>5? BLE InDone ;if j<=5, finish inner loop ... ;loop body SUB r2, r2, #1 ;j-- B InLoop InDone ADD r1, r1, #1 ;i++ B OutLoop OutDone
Aarya B.
4. Determine how many times the innermost loop will be iterated when the algorithm segment is implemented and run. (Assume that i, j, a, b, c, and d are all positive integers. for i := 5 to 50 for j := 10 to 20 [Statements in body of inner loop. None contain branching statements that lead outside the loop.] next j next i (a) Assume a ≤ b and c ≤ d. for i := a to b for j := c to d [Statements in body of inner loop. None contain branching statements that lead outside the loop.] next j next i (b)
Joseph D.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD