What is the main goal of applying strength reduction in compiler optimization?
Think about how replacing certain operations can make code run faster.
Strength reduction replaces costly operations like multiplication or division with simpler operations like addition or bit shifts, which are faster to execute.
Which of the following code transformations is an example of strength reduction?
for (int i = 0; i < n; i++) { a[i] = b * i; }
Consider how multiplication by i can be turned into addition inside the loop.
Strength reduction replaces multiplication inside the loop with an incremented variable that adds b each iteration, which is cheaper than multiplying every time.
Consider the loop below. What is the main performance benefit after applying strength reduction?
int x = 0; for (int i = 0; i < n; i++) { y[i] = 5 * i + 3; }
Focus on how multiplication inside the loop can be optimized.
By replacing multiplication with addition, the loop performs fewer costly operations, improving performance.
Which statement correctly distinguishes strength reduction from loop unrolling?
Think about what each optimization changes in the code.
Strength reduction focuses on replacing costly operations with cheaper ones, while loop unrolling reduces the overhead of looping by repeating the loop body multiple times.
Given the code snippet below, which option correctly applies strength reduction without changing the program's behavior?
int result = 0; for (int i = 1; i <= n; i++) { result += 4 * i; }
Consider how to replace multiplication with addition while preserving the sum.
Option A replaces multiplication by incrementing a temporary variable by 4 each iteration and adding it to result, preserving the original sum.