Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 13 / Topic 8 of 8

Putting it together: find and fix the slow method

Real-world example: a shop's nightly orders report (examples/OrdersData.java generates 8,000 fixed, repeatable orders; examples/SlowOrdersReport.java builds the report) takes longer than it should. The rule from section 6, applied for real: measure, don't guess.

The suspect method scans two growing lists for every single order:

static List<String> repeatCustomersSlow(List<OrdersData.Order> orders) {
    List<String> seen = new ArrayList<>();
    List<String> repeats = new ArrayList<>();
    for (OrdersData.Order order : orders) {
        String customer = order.customer();
        if (seen.contains(customer) && !repeats.contains(customer)) {   // scans both lists, every time
            repeats.add(customer);
        }
        seen.add(customer);
    }
    repeats.sort(null);
    return repeats;
}
$ javac OrdersData.java SlowOrdersReport.java
$ java SlowOrdersReport
Orders: 8000
Repeat customers: 650
  Chennai  Rs. 3,393,916.00
  Delhi    Rs. 3,515,460.00
  Jaipur   Rs. 3,409,831.00
  Kolkata  Rs. 3,469,696.00
  Mumbai   Rs. 3,375,197.00
  Pune     Rs. 3,625,163.00
Took 55 ms (varies by machine and run)

55 ms for 8,000 orders doesn't sound alarming on its own — which is exactly why guessing is the wrong tool here. examples/FindAndFixSlowMethod.java times each method separately to find out which one actually deserves the blame:

$ javac FindAndFixSlowMethod.java
$ java FindAndFixSlowMethod
== 1. Time the whole report first ==
Orders: 8000
Repeat customers: 650
  Chennai  Rs. 3,393,916.00
  Delhi    Rs. 3,515,460.00
  Jaipur   Rs. 3,409,831.00
  Kolkata  Rs. 3,469,696.00
  Mumbai   Rs. 3,375,197.00
  Pune     Rs. 3,625,163.00
Whole report: 45 ms

== 2. Time each method separately to find which one is slow ==
repeatCustomersSlow: 29 ms
totalByCity:         0 ms
repeatCustomersSlow is the one doing almost all the work.

repeatCustomersSlow is almost the entire 55 ms; totalByCity (one pass, one TreeMap.merge per order) is effectively free by comparison. List.contains is an O(n) scan, and both seen and repeats grow throughout the loop — scanning two growing lists for every one of 8,000 orders is the classic shape of an accidental O(n²). The fix counts every customer once, with a HashMap, so each check becomes a single O(1) hash lookup instead of a scan:

static List<String> repeatCustomersFast(List<OrdersData.Order> orders) {
    Map<String, Integer> counts = new HashMap<>();
    for (OrdersData.Order order : orders) {
        counts.merge(order.customer(), 1, Integer::sum);
    }
    List<String> repeats = new ArrayList<>();
    for (var entry : counts.entrySet()) {
        if (entry.getValue() >= 2) repeats.add(entry.getKey());
    }
    repeats.sort(null);
    return repeats;
}
== 3. Fix only repeatCustomersSlow, then check the answer is identical ==
Same result: true

== 4. Before and after, with warm-up so the JIT has compiled both ==
Before (list scan):  597 ms total (20 repeats)
After  (HashMap):    3 ms total
About 153.2x faster
(varies by machine and run — the ratio is what matters)
totalByCity was never the problem, so it was left exactly as it was.

Same result, confirmed before trusting the speed-up at all — then roughly 150 times faster, after 20 warmed-up repeats of each. totalByCity, which the measurement already showed was never the problem, was left exactly as it was: fixing code the profile didn't point at is wasted effort at best, and a new bug at worst.

Full programs: examples/OrdersData.java, examples/SlowOrdersReport.java, examples/FindAndFixSlowMethod.java.

Done here? The practice set for Part 13 — including the solutions — is on the part page.

Search the guide