Counting Operations
An operation is a single fundamental action your computer performs. Assignment, arithmetic, comparison, and array access each count as one operation. To predict how fast your code runs, count how many operations it executes.
What is an Operation?
Your computer processes code one step at a time. Common operations include assignment (x = 10), reading a variable, arithmetic (+, -, *, /), comparisons (<, >, ==), array access (arr[i]), and function calls. Each represents a single fundamental action.
The diagram shows code with each operation marked. Variable assignment takes one operation. Reading a variable and using it in arithmetic counts as two operations. Comparison and array access each count as one.
Counting in Simple Code
Consider a function that calculates an average. The first three lines assign variables—three operations. The sum calculation reads three variables and adds them—four operations. The division reads the sum, divides, and assigns—three operations. The return statement adds one more. Total: eleven operations inside the function, plus one for calling it.
1def calculate_average():
2 a = 10 # 1 operation
3 b = 20 # 1 operation
4 c = 30 # 1 operation
5 sum_val = a + b + c # 4 operations (read a, read b, read c, add twice, assign)
6 avg = sum_val / 3 # 3 operations (read sum_val, divide, assign)
7 return avg # 1 operation
8# Total: 11 operations
Loops Multiply Operations
A loop runs its body multiple times. If the body contains k operations and the loop runs n times, the total operation count is approximately n × k.
The diagram shows a loop that runs n times. Each iteration executes the operations inside the loop body. The operations outside the loop run once. The operations inside multiply by the number of iterations.
Consider a function that sums an array. Getting the array length takes one operation. Initializing the total takes another. The loop runs n times. Each iteration reads an array element, reads the total, adds them, and stores the result—three operations per iteration. The return statement adds one more. Total: 1 + 1 + (3 × n) + 1 = 3n + 3 operations.
1def sum_array(arr):
2 n = len(arr) # 1 operation
3 total = 0 # 1 operation
4 for i in range(n):
5 total += arr[i] # 3 operations per iteration
6 return total # 1 operation
7# Total: 3n + 3 operations
Nested Loops
Nested loops multiply operations across all levels. An outer loop running n times with an inner loop running n times creates n × n iterations. If each iteration has k operations, the total is n × n × k.
The diagram shows nested loops as a grid. The outer loop runs n times vertically. For each outer iteration, the inner loop runs n times horizontally. This creates n² total iterations. Each cell in the grid represents one execution of the inner loop body.
Consider a function that prints all pairs from an array. Getting the array length takes one operation. The outer loop runs n times. For each outer iteration, the inner loop runs n times. Each inner iteration reads two array elements and prints them—three operations. Total: 1 + (n × n × 3) = 3n² + 1 operations. For n = 5, this equals 76 operations. For n = 10, this jumps to 301 operations.
1def print_pairs(arr):
2 n = len(arr) # 1 operation
3 for i in range(n):
4 for j in range(n):
5 print(arr[i], arr[j]) # 3 operations
6# Total: 3n² + 1 operations
7# n=5: 76 operations
8# n=10: 301 operations
Operation Count Growing with Input Size
The diagram compares how operation counts grow. Simple code stays constant regardless of input size. A single loop grows linearly—doubling the input doubles the operations. Nested loops grow quadratically—doubling the input quadruples the operations. For n = 100, a nested loop executes 10,000 operations while a single loop executes only 100.
Why We Count
Counting operations predicts performance before running code. Comparing two algorithms shows which runs faster. Identifying bottlenecks reveals which parts slow down the program. This knowledge guides choosing the right approach for each problem.
Exact counts matter less than understanding growth. Does the count stay constant as input grows? Does it grow linearly with input size? Does it grow quadratically? Constant is best. Linear works well for most problems. Quadratic gets slow quickly. Exponential only handles tiny inputs.
The next lesson introduces Big-O notation, which formalizes this idea by categorizing algorithms based on their growth rate.