Testing and Debugging
Timing Probe
A deterministic counter can stand in for timing work when teaching how to compare paths.
Timing Probe
TimingProbe.java
public class TimingProbe {
public static void main(String[] args) {
int limit = ;
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 = ;
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 = ;
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);
}
}
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.