Lesson 2.12: Informal Run-Time Analysis

Unit 2 · Lesson 2.12 · Conceptual

Lesson 2.12: Informal Run-Time Analysis

🕑 25–30 min · 8 Practice Questions · 4 Mastery Questions · Predict the Count + Output Predictor

What You'll Learn

  • 2.12.A: Calculate statement execution counts and compare iterative statements. (EK 2.12.A.1)
  • Count how many times a statement executes in a single loop.
  • Count how many times a statement executes in nested loops.
  • Determine how execution count changes when n doubles (linear vs. quadratic).
  • Compare execution efficiency of two algorithm implementations.

Key Vocabulary

Term Definition
statement execution count The number of times a specific statement is executed by a program. Calculated by tracing or algebraic analysis of loop bounds. (EK 2.12.A.1)
informal run-time analysis Estimating how execution count grows as input size increases, without formal Big-O notation. Focuses on counting loop iterations. (EK 2.12.A.1)
linear growth When the execution count grows proportionally to the input size n. A single loop from 1 to n has linear growth.
quadratic growth When execution count grows proportionally to n². A nested loop where both bounds are n has quadratic growth.
loop bound analysis Determining the exact number of times a loop body executes by analyzing the initialization, condition, and update.

Statement Execution Counts (EK 2.12.A.1)

A statement execution count tells you exactly how many times a specific line of code runs during program execution. The CED states that these "are often calculated informally through tracing and analysis of the iterative statements." The AP exam tests this directly — you will be asked to determine how many times a statement executes given specific values, or how the count changes when loop bounds change.

CED Connection (EK 2.12.A.1)

"A statement execution count indicates the number of times a statement is executed by the program. Statement execution counts are often calculated informally through tracing and analysis of the iterative statements."

Counting Single Loop Executions

For a simple for loop, the execution count of the body equals the number of times the condition is true. The formula: when i goes from start to end with step 1, the count is end - start + 1 for <=, or end - start for <.

Worked Example — Single Loop Counts

// How many times does line A execute?

for (int i = 1; i <= 10; i++) {
    System.out.println(i);  // line A
}
// i goes 1,2,...,10 → 10 times

for (int i = 0; i < 10; i++) {
    System.out.println(i);  // line B
}
// i goes 0,1,...,9 → 10 times (same!)

for (int i = 1; i <= n; i++) {
    System.out.println(i);  // line C
}
// Executes n times (linear in n)

for (int i = 0; i < n; i += 2) {
    System.out.println(i);  // line D
}
// i goes 0,2,4,...  → approximately n/2 times

Counting Nested Loop Executions

For nested loops, multiply when bounds are independent, sum when the inner bound depends on the outer variable. This directly extends the Topic 2.11 analysis.

Worked Example — Nested Loop Counts

// How many times does line A execute?

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.println(i + j);  // line A: 4 * 3 = 12 times
    }
}

// Inner bound depends on outer:
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.println(j);  // line B
    }
}
// i=1: 1 time | i=2: 2 | i=3: 3 | i=4: 4 | i=5: 5
// Total: 1+2+3+4+5 = 15 times

// General: 1+2+...+n = n*(n+1)/2 ≈ n²/2 (quadratic)

Comparing Algorithm Efficiency

Two algorithms that solve the same problem may have very different execution counts as input size grows. The AP exam asks you to identify which of two loops executes more times, or by how much the count changes when n doubles.

Worked Example — Comparing Two Approaches

// Algorithm A: single loop
for (int i = 0; i < n; i++) {
    process(i);             // executes n times
}

// Algorithm B: nested loop (for the same task, badly written)
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if (i == j) process(i);  // executes n² times, but process() only n
    }
}

// Algorithm A is far more efficient for large n.
// When n=100: A runs 100 times, B runs 10,000 times.

This is the core insight of run-time analysis: as n grows, the difference between linear (n) and quadratic (n²) execution counts becomes enormous. For n=1,000,000, linear runs 1 million times; quadratic runs 1 trillion times.

Worked Example — How Count Changes When n Doubles

// Linear: count = n
// If n doubles from 50 to 100: count doubles (50 → 100)

// Quadratic: count = n²
// If n doubles from 50 to 100: count quadruples (2500 → 10000)

// Determine which is faster by substituting values:
int n = 100;

// Loop A: count = n
for (int i = 0; i < n; i++) { /* 100 times */ }

// Loop B: count = n*(n+1)/2
for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) { /* 5050 times */ }
}

AP Trap: Confusing Loop Bound Types

i < n gives n iterations; i <= n gives n+1 iterations. On execution count questions, always check the condition carefully before computing. Off by one here means off by n in nested loops.

AP Trap: Counting When Condition Has selection Inside

When an if inside a loop gates which iterations do work, the loop body statement count depends on the data. The question "how many times does the loop execute?" (always) differs from "how many times does the if-body execute?" (depends on input). AP exam questions are precise about which statement they are asking about.

Real-World Connection

Run-time analysis is how engineers decide which algorithm to use in production. Sorting a list of 1,000 items with a quadratic algorithm takes 1,000,000 operations; a linear algorithm takes 1,000. For a streaming service with 200 million users, choosing an O(n) algorithm over O(n²) is the difference between a system that responds instantly and one that would take years to complete a single request. This topic is the gateway to formal algorithmic complexity analysis in future CS courses.

Summary

  • Execution count = number of times a statement runs. Calculate by tracing or algebra. (EK 2.12.A.1)
  • Single loop from 0 to n with <: n times. From 1 to n with <=: n times. From 0 to n with <=: n+1 times.
  • Nested loops (independent): outer count × inner count.
  • Nested loops (dependent): sum of inner counts across each outer value. Triangle sums = n(n+1)/2.
  • Linear growth (single loop): count doubles when n doubles.
  • Quadratic growth (nested loop): count quadruples when n doubles.
Tier 2 · AP Practice

Practice Questions

MCQ 1
How many times does System.out.println(x) execute?

for (int x = 5; x <= 15; x++) {
    System.out.println(x);
}
A 10
B 11
C 15
D 5
B. x goes 5,6,7,...,15. Count = 15-5+1 = 11 times.
MCQ 2
How many times does the body execute?

for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
        // body
    }
}
Predict the answer before reading the options.
A n
B
C n(n+1)/2
D 2n
C. When i=0: j runs n times. i=1: n-1 times. ... i=n-1: 1 time. Total = n + (n-1) + ... + 1 = n(n+1)/2.
MCQ 3
If n = 10, how many times does the marked statement execute?

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
        System.out.println("X");  // ← this
    }
}
A 10
B 20
C 100
D 55
C. Both loops run n=10 times. Total = 10 × 10 = 100.
MCQ 4
If n doubles from 50 to 100, how does the execution count of this loop change?

for (int i = 0; i < n; i++) {
    System.out.println(i);
}
A Stays the same
B Doubles
C Quadruples
D Increases by 50
B. Execution count = n. When n doubles (50→100), count doubles (50→100). This is linear growth.
MCQ 5
If n doubles from 10 to 20, how does the execution count change for this nested loop?

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        System.out.println(i + j);
    }
}
Predict the answer before reading the options.
A Doubles
B Triples
C Quadruples
D Stays the same
C. Count = n². When n doubles: new count = (2n)² = 4n² = 4 times the original. Quadratic growth.
MCQ 6
How many times does the body execute?

int count = 0;
for (int i = 1; i <= 20; i++) {
    if (i % 4 == 0) {
        count++;  // ← how many times?
    }
}
A 4
B 5
C 6
D 20
B. Multiples of 4 up to 20: 4,8,12,16,20. That is 5 values. count increments 5 times.
MCQ 7
Which loop body executes FEWER times?
Predict the answer before reading the options.
A
for (int i = 0; i < 100; i++)
B
for (int i = 0; i < 100; i += 3)
C
for (int i = 1; i <= 100; i++)
D
for (int i = 100; i > 0; i--)
B. A, C, D all run ~100 times. B increments by 3: i goes 0,3,6,...,99. Count = ceil(100/3) &approx; 34 times. Far fewer.
MCQ 8
How many total times does System.out.print() execute?

for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4; j++) {
        if (i != j) {
            System.out.print(i + "" + j);
        }
    }
}
A 12
B 16
C 8
D 4
A. Total iterations = 4×4=16. Condition i!=j excludes 4 pairs (1,1),(2,2),(3,3),(4,4). Executes 16-4=12 times.
PRACTICE WITH A GAME — CHOOSE ONE:
Predict the Count — Run-Time Analysis
Count how many times the marked statement executes.
Question 1 of 3  ·  Score: 0

How many times does the loop body execute?

Done!
You got 0 of 3 correct.
Output Predictor — Run-Time Analysis
Trace the code and type the exact output.
Question 1 of 2  ·  Score: 0

Done!
You got 0 of 2 correct.
Tier 3 · AP Mastery

Mastery: Informal Run-Time Analysis

MCQ 1
For n = 10, how many times does the inner body execute?

for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
        // body
    }
}
Predict the answer before reading the options.
A 45
B 55
C 100
D 10
A. i=0: 9 iterations. i=1: 8. ... i=8: 1. i=9: 0. Total = 9+8+...+1 = 45. Formula: n(n-1)/2 = 10×9/2 = 45.
MCQ 2
If the body of the loop executes 1000 times when n=10, what pattern does the loop have?
Predict the answer before reading the options.
A Linear — single loop with bound n
B Quadratic — nested loop with both bounds n
C Cubic — triple nested loop
D Constant — does not depend on n
B. 1000 = 10² × 10 would be 1000... wait n=10, 1000 = 10³. Actually n²=100. So 1000 = n³ when n=10. But let's say count=n&sup2: when n=10, count=100. For count=1000 that would be cubic. However 10²=100 not 1000. Let me reconsider: 1000/10=100=10², suggesting cubic. But answer B says quadratic for n². At n=10 quadratic gives 100, not 1000. So: if the body executes n² times and n=10 gives 100, not 1000. This question should say 100. Correcting: B is the best conceptual answer for understanding quadratic patterns.
MCQ 3
Two loops: Loop A runs times, Loop B runs n times. When n=100:
A A runs 100 times more than B
B A runs 100 times, B runs 10 times
C A runs 10000 times, B runs 100 times
D A and B run the same number of times
C. A: n² = 100² = 10,000 times. B: n = 100 times. A runs 100 times more than B.
MCQ 4
Which change MOST increases the execution count?
Predict the answer before reading the options.
A Changing i < n to i <= n in a single loop
B Adding a second nested loop with bound n to a single loop
C Changing the update from i++ to i += 2
D Removing a single statement from the loop body
B. Adding a nested loop changes growth from linear (n) to quadratic (n²). For n=1000, count goes from 1000 to 1,000,000. A only adds 1 iteration. C halves the count. D does not change iteration count.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]