Counting Work Saved by Short-Circuit Evaluation
A Boolean search can return once its answer is determined. Count elements evaluated rather than elements in the original source.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a universal check
For positive checks on 6, 2, minus 1, and 8, the check for all stops at minus 1 and visits three elements. Counting every predicate would predict four. Any and all define when the answer becomes final.
Estimate cost under a particular order
If a costly independent subtest can safely be guarded by a cheap one, place the guard first when it often rules out the operation. Preserve intended effects and input rules under short-circuit evaluation.[1]
Put an inexpensive selective check before an expensive one
To test whether a record contains a valid optional payload, first check that the field exists, then parse it. On one hundred inputs with only ten payloads, Python's short-circuit conjunction makes ten parser calls when every input reaches this condition. See short-circuit evaluation.
Input category presence checks parser calls
90 missing 90 0
10 present 10 10
Total 100 10
Reorder only when observable behavior permits
If one predicate updates metrics or can raise, moving it can change which effects happen. Tests should verify call counts and returned results separately. Include an input that fails the first condition and would raise if the second were evaluated.