Exercise 3: Real Wall-Clock Time at Real Clock Speeds — Possible Solution ==================================================================== THE FORMULA ------------------------------ Real time per iteration = (cycles per iteration) / (clock speed in cycles per second) 6502 AT ~1MHz ------------------------------ Cycles per iteration: 19 Clock speed: 1MHz = 1,000,000 cycles per second Time = 19 / 1,000,000 seconds = 0.000019 seconds = 19 microseconds (19 us) Z80 AT ~3.5MHz ------------------------------ Cycles per iteration: 26 Clock speed: 3.5MHz = 3,500,000 cycles per second Time = 26 / 3,500,000 seconds ≈ 0.0000074286 seconds ≈ 7.43 microseconds (7.43 us) WHICH ONE IS ACTUALLY FASTER ------------------------------ The Z80 is actually faster in real wall-clock time — roughly 7.43 us per iteration versus the 6502's 19 us, even though the Z80 needed MORE raw cycles (26 vs. 19) to do the same work. The Z80's much higher clock speed (3.5MHz vs. roughly 1MHz) more than makes up for needing more cycles per iteration — it's completing over 2.5x more clock ticks per second, which outweighs needing about 37% more ticks per loop pass. WHY RAW CYCLE COUNTS ALONE WOULD HAVE BEEN MISLEADING ------------------------------ Looking only at "19 cycles vs. 26 cycles," as this chapter's own comparison table presents by itself, would suggest the 6502 does this task in less "effort" — and by the narrow measure of cycles per iteration, that's true. But a cycle isn't a fixed unit of real time — it's whatever one clock tick happens to take on that specific chip, running at that specific speed in that specific machine. Comparing cycle counts across two chips with genuinely different clock speeds, without also accounting for those clock speeds, is exactly the trap this chapter's own warn-box names: "comparing two different units and reading them as if they were one." WHY THIS WORKS AS AN ANSWER ------------------------------ It performs the actual wall-clock calculation for both chips using the given cycle counts and clock speeds, correctly identifies the Z80 as faster in real time despite needing more cycles, and explains precisely why the raw cycle-count comparison alone would have led to the wrong practical conclusion.