Course contentsShow
← Computer Science
Lesson 271 of 2,8727. Introduction to Complexity AnalysisFree lesson

Counting Primitive Operations

Learning to identify and count basic operations like assignments, comparisons, and arithmetic operations.

Counting Primitive Operations

What you'll learn: How to identify and tally the basic operations your code performs to measure its computational cost.

The Core Idea

When analyzing how efficient an algorithm is, we need a standard way to measure "work done." Instead of timing code execution (which varies by computer speed), we count primitive operations—the simplest, indivisible actions a computer performs.

Think of it like counting steps in a recipe. Whether you're a fast cook or slow cook, "crack an egg" is one step. Similarly, whether your computer is fast or slow, certain basic operations count as one unit of work.

What Counts as a Primitive Operation?

Common primitive operations include:

  • Assignments: x = 5
  • Arithmetic operations: a + b, x * y, n - 1
  • Comparisons: i < n, x == y
  • Array access: arr[i]
  • Function calls/returns (excluding the work inside the function)

Example Count

sum = 0           // 1 operation (assignment)
i = 0             // 1 operation (assignment)
i < n             // 1 operation (comparison)
arr[i]            // 1 operation (array access)
sum + arr[i]      // 1 operation (addition)
sum = sum + arr[i] // 1 operation (assignment)
i = i + 1         // 2 operations (addition + assignment)

Notice we count each distinct action. The statement sum = sum + arr[i] actually contains multiple operations: array access, addition, and assignment.

Why This Matters

By counting primitive operations systematically, we can compare algorithms objectively. An algorithm with 10 operations is predictably faster than one with 1000 operations, regardless of the machine running them.

Key Takeaway: Primitive operations are the atomic building blocks we count to measure an algorithm's work—assignments, comparisons, arithmetic, and array accesses each count as one unit.