In: Computer Science
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5…
int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; }
a) Accounting for delay slots, what is the dynamic instruction count of this routine? What is the dynamic instruction count if you add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level)? Assuming all instructions have the same CPI, what is the speedup?
Solution: Dynamic instructions are referred to as those instructions present within a program that is actually executed by the CPU to implement a certain functionality within the code, for example, for-loop, while-loop, do-while loop, etc. On the other hand, the instructions that are executed by the CPU for the normal functionalities such as declaring a variable, printing a value, and initializing the variable are known as static instructions.
Now, coming to the code that is being mentioned-above, the number of dynamic instructions is: 4
int i = 0; -Dynamic Instruction 1 i < n; -Dynamic Instruction 2 i++ -Dynamic Instruction 3 sum += i; -Dynamic Instruction 4
This because all these instructions are executed within the loop.
On the other hand, the number of static instructions within the code is: 3
int summarize(int n) -Static Instruction 1 int sum = 0; -Static Instruction 2 int sum = 0; -Static Instruction 3
This is because all of these instructions are executed to implement the normal functionalities.
Here's the solution to your question, please provide it a 100% rating. Thanks for asking and happy learning!!