2.12 - Informal Run-Time Analysis

mg8mer

Introduction

Welcome to the FiveHive article for topic 2.12 of AP CSA! 

In this article, we will briefly cover the learning objective of 2.12  as shown in the AP® Computer Science A Course and Exam Description 2025.

2.12.A - Calculate statement execution counts and informal run-time comparison of iterative statements.

This is our last article of Unit 2! Hooray! And fortunately for you this is gonna be a short article with simple practice!

We’re gonna cover what informal run-time analysis actually is, look at an example, and you’ll try two out on your own!

Let’s finish strong! Get excited!

Informal Run-Time Analysis

CollegeBoard here is trying to get you to analyze and evaluate the efficiency of algorithms. Typically, you would do that by seeing how LONG it takes to run via external methods, but let’s not worry about that for now.

The reason why it’s “informal” is because you’re doing so by simply counting how many times a loop/nested loop iterates, and the number of times they execute some statement is referred to as the statement execution count. As stated by CollegeBoard, this is calculated by tracing the execution, which is a fundamentally informal process.

Guided Practice

Let’s take a look at how this might look before I send you off on your own.

How many times does the below nested for loop execute?

for (int i = 0; i < 10; i++)
{
	for (int j = 0; j < 5; j++)
	{
		System.out.println(“#”);
	}
}

Think about it on your own first!

Ok let’s see. In the first loop, “i” is set to 0, and increments by one each iteration so long as it isn’t equal to 10. In that case, the outer loop iterates ten times.

Now for the inner loop, “j” is 0, and increments by one until “j” equals 5. So the inner loop iterates five times.

Now let’s think of it this way: the outer loop iterates ten times, and for EACH time it iterates, the inner loop iterates 5 times. So you can get the total iterations by just multiplying 5 and 10, giving you a statement execution count of 50.

Simple enough? Try two on your own!