Testing and Debugging
Timing Probe
A deterministic counter can stand in for timing work when teaching how to compare paths.
probe counter
A counter records how many loop steps were needed without relying on wall-clock time.
comparable paths
The two loops do the same kind of work, so their counts can be compared directly.
Timing Probe
TimingProbe.java
Replay: real traced execution (multi-file project)
public class TimingProbe {
public static void main(String[] args) {
int limit = 4;
int slowSteps = 0;
int fastSteps = 0;
for (int i = 0; i < limit; i++) {
slowSteps++;
}
for (int i = 0; i < limit; i += 2) {
fastSteps++;
}
System.out.println("slow=" + slowSteps + " fast=" + fastSteps);
}
}
public class TimingProbe {
public static void main(String[] args) {
int limit = 2;
int slowSteps = 0;
int fastSteps = 0;
for (int i = 0; i < limit; i++) {
slowSteps++;
}
for (int i = 0; i < limit; i += 2) {
fastSteps++;
}
System.out.println("slow=" + slowSteps + " fast=" + fastSteps);
}
}
public class TimingProbe {
public static void main(String[] args) {
int limit = 6;
int slowSteps = 0;
int fastSteps = 0;
for (int i = 0; i < limit; i++) {
slowSteps++;
}
for (int i = 0; i < limit; i += 2) {
fastSteps++;
}
System.out.println("slow=" + slowSteps + " fast=" + fastSteps);
}
}
limit ← 4, slowSteps ← 0, fastSteps ← 0
1public class TimingProbe {2 public static void main(String[] args) {3 int limit→ 4 = 4; //@limit=2, 64 int slowSteps→ 0 = 0;5 int fastSteps→ 0 = 0;slowSteps ← 1
pass 1 of 47for (int i0 = 0; i < limit4; i++) {8 slowSteps→ 1++;9}All 4 passes — pass 1 is the card above pass islowStepsfastSteps1 0 0 → 1 — 2 1 1 → 2 — 3 2 2 → 3 — 4 3 3 → 4 0 → 1 fastSteps ← 1
pass 1 of 211for (int i0 = 0; i < limit4; i += 2) {12 fastSteps→ 1++;13}fastSteps ← 2
pass 2 of 211for (int i2 = 0; i < limit4; i += 2) {12 fastSteps→ 2++;13}System.out.println("slow=" + slowSteps + " fast=" + fastSteps);
15 System.out.println("slow=" + slowSteps4 + " fast=" + fastSteps2);16}outputslow=4 fast=2
limit ← 2, slowSteps ← 0, fastSteps ← 0
1public class TimingProbe {2 public static void main(String[] args) {3 int limit→ 2 = 2;4 int slowSteps→ 0 = 0;5 int fastSteps→ 0 = 0;slowSteps ← 1
pass 1 of 27for (int i0 = 0; i < limit2; i++) {8 slowSteps→ 1++;9}slowSteps ← 2
pass 2 of 27for (int i1 = 0; i < limit2; i++) {8 slowSteps→ 2++;9}fastSteps ← 1
11for (int i0 = 0; i < limit2; i += 2) {12 fastSteps→ 1++;13}System.out.println("slow=" + slowSteps + " fast=" + fastSteps);
15 System.out.println("slow=" + slowSteps2 + " fast=" + fastSteps1);16}outputslow=2 fast=1
limit ← 6, slowSteps ← 0, fastSteps ← 0
1public class TimingProbe {2 public static void main(String[] args) {3 int limit→ 6 = 6;4 int slowSteps→ 0 = 0;5 int fastSteps→ 0 = 0;slowSteps ← 1
pass 1 of 67for (int i0 = 0; i < limit6; i++) {8 slowSteps→ 1++;9}All 6 passes — pass 1 is the card above pass islowSteps1 0 0 → 1 2 1 1 → 2 3 2 2 → 3 4 3 3 → 4 5 4 4 → 5 6 5 5 → 6 fastSteps ← 1
pass 1 of 311for (int i0 = 0; i < limit6; i += 2) {12 fastSteps→ 1++;13}All 3 passes — pass 1 is the card above pass ifastSteps1 0 0 → 1 2 2 1 → 2 3 4 2 → 3 System.out.println("slow=" + slowSteps + " fast=" + fastSteps);
15 System.out.println("slow=" + slowSteps6 + " fast=" + fastSteps3);16}outputslow=6 fast=3