# SPDX-License-Identifier: MIT """Speed of ``run()`` on a running chip: best of N runs by CPU time. python -I scripts/speed.py # CPython or PyPy, whichever runs it All four channels sound (two noise, one gated square, one high-passed tone), so no part of the chip idles. Reference cores are correct, not fast: this is a measurement for the boards' planning, not a target. """ from __future__ import annotations import argparse import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from pokey_python import POKEY # noqa: E402 NTSC = 1_789_773 def running_chip() -> POKEY: chip = POKEY() for register, value in ( (0, 40), (1, 0x2A), (2, 3), (3, 0xCA), (4, 90), (5, 0x0A), (6, 7), (7, 0xA9), (8, 0x64), (15, 3), (9, 0), ): # fmt: skip chip.write(register, value) chip.tick() return chip def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--cycles", type=int, default=1_000_000) parser.add_argument("--runs", type=int, default=5) args = parser.parse_args() chip = running_chip() chip.run(200_000) # warm-up (PyPy's JIT) best = None for _ in range(args.runs): began = time.process_time() chip.run(args.cycles) spent = time.process_time() - began best = spent if best is None else min(best, spent) rate = args.cycles / best print(f"{sys.implementation.name} {sys.version.split()[0]}: {rate / 1e6:.2f} M cycles/s " f"({rate / NTSC:.2f}x NTSC real time), best of {args.runs} x {args.cycles:,}") return 0 if __name__ == "__main__": sys.exit(main())