In each of the following C code snippets, there are issues that can prevent the compiler from applying certain optimizations. For each snippet: Circle the line number that contains compiler optimization blocker. ⚫ Select the best modification to improve optimization. 1. Which line prevents compiler optimization? Circle one: 2 3 4 5 6 Suggested solution: ⚫ Remove printf or move it outside the loop. Remove the loop. • Replace arr[i] with a constant value. 1 int sum (int *arr, int n) { 2 int s = 0; 3 for (int i = 0; i < n; i++) { 4 5 6 } 7 8 } s = arr[i]; printf("%d\n", s); return s; 234206 2. Which line prevents compiler optimization? Circle one: 2 3 4 5 6 Suggested solution: Move or eliminate do_extra_work() if it's not necessary inside the loop. Remove the loop (but what about scaling?). ⚫ Replace arr[i] *= factor; with arr[i] = 0; (why would that help?). 1 void scale (int *arr, int n, int factor) { 5 6 } for (int i = 0; i < n; i++) { rr[i] = factor; do_extra_work ();
In each of the following C code snippets, there are issues that can prevent the compiler from applying certain optimizations. For each snippet: Circle the line number that contains compiler optimization blocker. ⚫ Select the best modification to improve optimization. 1. Which line prevents compiler optimization? Circle one: 2 3 4 5 6 Suggested solution: ⚫ Remove printf or move it outside the loop. Remove the loop. • Replace arr[i] with a constant value. 1 int sum (int *arr, int n) { 2 int s = 0; 3 for (int i = 0; i < n; i++) { 4 5 6 } 7 8 } s = arr[i]; printf("%d\n", s); return s; 234206 2. Which line prevents compiler optimization? Circle one: 2 3 4 5 6 Suggested solution: Move or eliminate do_extra_work() if it's not necessary inside the loop. Remove the loop (but what about scaling?). ⚫ Replace arr[i] *= factor; with arr[i] = 0; (why would that help?). 1 void scale (int *arr, int n, int factor) { 5 6 } for (int i = 0; i < n; i++) { rr[i] = factor; do_extra_work ();
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 20RQ
Related questions
Question
![In each of the following C code snippets, there are issues that can prevent the compiler
from applying certain optimizations. For each snippet:
Circle the line number that contains compiler optimization blocker.
⚫ Select the best modification to improve optimization.
1. Which line prevents compiler optimization? Circle one: 2 3 4 5 6
Suggested solution:
⚫ Remove printf or move it outside the loop.
Remove the loop.
• Replace arr[i] with a constant value.
1 int sum (int *arr, int n) {
2
int s = 0;
3
for (int i
=
0; i < n; i++) {
4
5
6
}
7
8 }
s = arr[i];
printf("%d\n", s);
return s;
234206
2. Which line prevents compiler optimization? Circle one: 2 3 4 5 6
Suggested solution:
Move or eliminate do_extra_work() if it's not necessary inside the loop.
Remove the loop (but what about scaling?).
⚫ Replace arr[i] *= factor; with arr[i] = 0; (why would that help?).
1 void scale (int *arr, int n, int factor) {
5
6 }
for (int i = 0; i < n; i++) {
rr[i] = factor;
do_extra_work ();](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3b25bdcc-1f5e-4716-88d3-d1e15ee2df25%2Fba745930-7478-4737-a933-8f10111ecd4f%2Fq56svp8_processed.jpeg&w=3840&q=75)
Transcribed Image Text:In each of the following C code snippets, there are issues that can prevent the compiler
from applying certain optimizations. For each snippet:
Circle the line number that contains compiler optimization blocker.
⚫ Select the best modification to improve optimization.
1. Which line prevents compiler optimization? Circle one: 2 3 4 5 6
Suggested solution:
⚫ Remove printf or move it outside the loop.
Remove the loop.
• Replace arr[i] with a constant value.
1 int sum (int *arr, int n) {
2
int s = 0;
3
for (int i
=
0; i < n; i++) {
4
5
6
}
7
8 }
s = arr[i];
printf("%d\n", s);
return s;
234206
2. Which line prevents compiler optimization? Circle one: 2 3 4 5 6
Suggested solution:
Move or eliminate do_extra_work() if it's not necessary inside the loop.
Remove the loop (but what about scaling?).
⚫ Replace arr[i] *= factor; with arr[i] = 0; (why would that help?).
1 void scale (int *arr, int n, int factor) {
5
6 }
for (int i = 0; i < n; i++) {
rr[i] = factor;
do_extra_work ();
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps

Recommended textbooks for you

EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT

C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,

EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT

C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,

C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage

New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning