Informal Run-Time Analysis
- Informal Code Analysis
- What IS informal code analysis?
Informal Code Analysis
Learning objective: Compute statement execution counts & informal run-time comparison of iterative statements
Essential Knowledge: A statement execution count indicates the number of times a statement is executed by the program
What IS informal code analysis?
Answer:
// CODE EXAMPLE #1 (for loop)
public class InformalCodeAnalysis {
public static void main(String[] args) {
int count = 0;
for (int k = 0; k < 30; k++)
{
if (k % 3 == 0) // statement 1
{
count++; // statement 2
}
}
}
}
How many times will statement 1 execute?
Answer:
How many times will statement 2 execute?
Answer:
<b>How many times will statement 3 execute?</b>
Answer:
// Rewrite the code segment below to have a faster run-time based on statement execution counts
for (int k = 0; k < 135; k++)
{
if (k % 5 == 0)
{
System.out.println(k);
}
}
// CODE EXAMPLE #3 (while loop)
int num = (int)(Math.random() * 10);
while (num % 2 != 0)
{
num = (int)(Math.random() * 10) // statement 4
}
<b>What is the min/max number of times statement 4 will execute?</b>
Answer:
// CODE EXAMPLE #4 (nested loop)
for (int outer = 0; outer < 3; outer++)
{
for (int inner = 0; inner < 4; inner++)
{
// statement #5
}
}