Correctness of Programs
Efficiency is important, but it is secondary to correctness, and a program that is not correct is not efficient regardless of its speed.
Judging a Program by Its Speed
Many beginning programmers judge a program by how quickly it finishes. If one version runs in a second and another takes a minute, the faster one feels like the better one. This instinct is natural, and it is also the source of a common mistake. Speed is a real property worth studying, but it is not the first property we should check. Before asking how fast a program is, we should ask whether it does its job at all.
What do you think happens?
Is efficiency more important than correctness?
Reveal answer
Efficiency is important, but it is secondary to correctness. A program that is not correct is not efficient, regardless of its speed.
A fast program looks like progress because its output arrives sooner, but that output is only worth something if it is right.
Tracing Two Programs Side by Side
Consider two hypothetical programs written for the same task. Program A finishes in one second but returns the wrong answer. Program B takes a full minute but returns the correct answer. Follow each one through the two questions a careful reviewer asks, in order: does it do its job, and then how fast does it do it?
Program A wins on speed, but that win does not make it efficient. Its speed was obtained by failing the one thing it was supposed to deliver. Program B is slower, yet it has actually done the job. The source's position is that a program that is not correct is not efficient regardless of its speed, so Program B is the one that can be called efficient in the sense that matters, and it still must be judged on how long that minute is compared with what the task needs.
Why Correctness Is the Entry Requirement
The source ranks these two concerns clearly. Efficiency is important, but it is secondary to correctness. The reasoning works in both directions, and it is worth looking at each direction separately.
In the first direction, suppose you do not care whether a program works. You can make it run very quickly indeed, but nobody would consider that a meaningful achievement. The speed was bought by giving up the one thing the program was supposed to deliver.
The reverse failure is also real. A program that produces a correct answer after ten thousand years is not very useful either. So efficiency is not optional. A correct program that takes an impractically long time still has a problem.
Tracing the Order of Checks
The difference between the belief most learners start with and the correct order of evaluation is easiest to see by tracing what happens to the same fast, wrong program under each ordering. Under the speed-first habit, the program passes because it finishes quickly. Under the correctness-first habit, the program is stopped before its speed is ever considered.
Resources That Efficiency Can Describe
The word efficiency is broader than speed. It can describe the efficient use of almost any resource a computer has. The source names four: time, computer memory, disk space, and network bandwidth. A program can be efficient with one of these and wasteful with another, so when someone says a program is efficient, the useful follow-up question is which resource they mean.
Time Efficiency and Its Central Question
This article, and the analysis that follows it in the chapter, concentrates on time efficiency. Time efficiency refers to how long a program takes to execute. Keeping one resource in focus makes the analysis manageable, and the source explicitly limits itself to this case.
Time efficiency asks how long a program takes to perform the task it was written for. Stated as a question about a program: how long does it take to do its job, and is there another approach that will get the same result more quickly?
Notice that the question is phrased around a task, not around the program's code or its raw speed in isolation. The two follow-up questions, how long the program runs and whether a better method exists, both ask the same thing from different angles: whether a better method exists for the same result.
Applying the central question to a single task
A program is written to find the largest number in a list of values. Apply the central question of time efficiency to it.
- 1Check correctness first
Confirm the program actually returns the largest value. If it returns something else, the question of speed does not arise, because a program that is not correct is not efficient.
- 2Identify the task
The task is finding the largest value. The analysis is about how long the program takes to do that job.
- 3Ask how the time grows
Consider how the running time changes as the list gets longer. This is the growth the central question is concerned with.
- 4Ask whether a better method exists
Consider whether another approach would get the same largest value more quickly. Both follow-up questions point at the same comparison of methods.
The central question is answered only after correctness is established. Time efficiency then asks how long the verified program takes for its task and whether a better method for the same result exists.
Mistakes Beginners Make
Common mistake Treating a faster program as the better program regardless of its output
What it looks likeChoosing a version that finishes in one second over one that takes a minute, even though the fast version returns wrong results.
Why it failsSpeed is only worth something when the program does its job. A fast wrong answer has not made progress.
Do this insteadCheck whether the program does its job first, then compare how long the correct versions take.
Common mistake Assuming efficiency means only speed
What it looks likeJudging a program efficient because it runs quickly, without asking whether it uses memory, disk space, or network bandwidth wastefully.
Why it failsThe word efficiency can refer to several resources, and a program can be efficient with one and wasteful with another.
Do this insteadWhen someone calls a program efficient, ask which resource they mean.
Common mistake Dismissing efficiency entirely once a program is correct
What it looks likeAccepting a correct program that would take ten thousand years to produce its answer.
Why it failsCorrectness is the entry requirement, but a correct program that takes an impractically long time still has a real efficiency problem.
Do this insteadTreat efficiency as the question of whether a correct program is usable in practice.
Practice: Ranking Two Programs
Program X returns correct results and finishes in one minute. Program Y finishes in one second but sometimes returns wrong results. Which program would you choose, and what two questions did you ask to reach that decision, in what order?
Hints
- Start with the property the source ranks higher.
- Ask whether Program Y does its job before comparing how long each one takes.
- A correct program that takes too long still has an efficiency problem, so consider both properties.
Key Takeaways
- Efficiency matters, but it is secondary to correctness: a program that is not correct is not efficient, however fast it runs.
- The reasoning works in both directions. Speed bought by giving up the correct result is not an achievement, and a correct answer that arrives after an impractical wait is not very useful.
- The word efficiency can refer to time, computer memory, disk space, or network bandwidth, so the useful follow-up question is always which resource is meant.
- This article focuses on time efficiency, which asks how long a program takes to perform the task it was written for, and whether a better method exists for the same result.