#!/usr/bin/env python3 """Run with Python 3.14+ on macOS arm64: python3 replay.txt --output-dir NEW_DIRECTORY. Installs a pinned public CPU wheel in TemporaryDirectory outside the source workspace. The output directory must not exist. No catalog edits, credentials, GPU, or cloud inference. """ import argparse import hashlib import json import os from pathlib import Path import platform import subprocess import sys import tempfile import time import urllib.request import venv VERSION = '3.14.3' SEED = 20261103 def dump(path, value): with Path(path).open('x', encoding='utf-8') as handle: json.dump(value, handle, ensure_ascii=False, indent=2, allow_nan=False) handle.write('\n') def digest(value): return hashlib.sha256(json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(',', ':'), allow_nan=False).encode()).hexdigest() def worker(output): import difflib import random import statistics import unicodedata # Explicit native CPU implementation. RapidFuzz has no Metal initialization here. os.environ['RAPIDFUZZ_IMPLEMENTATION'] = 'cpp' start = time.perf_counter_ns() import rapidfuzz from rapidfuzz import fuzz import_ns = time.perf_counter_ns() - start assert rapidfuzz.__version__ == VERSION assert 'cpp' in fuzz.ratio.__module__, fuzz.ratio.__module__ began = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) def corpus(name, language, n): rng = random.Random(SEED + (0 if language == 'en' else 1)) if language == 'en': brands = ['Maple', 'River', 'Cloud', 'Amber', 'Cedar', 'Blue', 'Silver', 'Lake'] items = ['Notebook', 'Cable', 'Lamp', 'Bottle', 'Case', 'Adapter', 'Pen', 'Stand'] colors = ['red', 'green', 'blue', 'white', 'black', 'gold'] else: brands = ['あおぞら', 'こもれび', 'みなと', 'こはく', 'つばさ', 'しらゆき', 'はるかぜ', 'かえで'] items = ['ノート', 'ケーブル', 'ライト', 'ボトル', 'ケース', 'アダプター', 'ペン', 'スタンド'] colors = ['赤', '緑', '青', '白', '黒', '金'] candidates = [f'{rng.choice(brands)} {rng.choice(items)} {rng.choice(colors)} ' + ''.join(rng.choices('ABCDEFGHJKLMNPQRSTUVWXYZ23456789', k=6)) for _ in range(n)] assert len(set(candidates)) == n queries = [] for i in range(16): source = (i * 61 + 7) % n text = candidates[source] mode = ['exact', 'delete_middle', 'swap_middle', 'width_and_case'][i % 4] mid = len(text) // 2 if mode == 'delete_middle': text = text[:mid] + text[mid + 1:] elif mode == 'swap_middle': text = text[:mid] + text[mid + 1] + text[mid] + text[mid + 2:] elif mode == 'width_and_case': text = ''.join(chr(ord(c.lower()) + 0xFEE0) if '!' <= c <= '~' else '\u3000' if c == ' ' else c for c in text) queries.append({'id': f'q{i:02}', 'text': text, 'mutation': mode, 'source_candidate_index': source}) return {'id': name, 'language': language, 'candidates': candidates, 'queries': queries} corpora = [corpus('en100', 'en', 100), corpus('en1000', 'en', 1000), corpus('ja1000', 'ja', 1000)] inputs = {'seed': SEED, 'corpora': corpora} dump(output / 'inputs.json', inputs) score_functions = { 'rapidfuzz': lambda a, b: fuzz.ratio(a, b, processor=None), 'difflib': lambda a, b: difflib.SequenceMatcher(None, a, b, autojunk=False).ratio(), } def evaluate(tool, queries, candidates): scorer = score_functions[tool] rows = [] for query in queries: scores = [scorer(query, candidate) for candidate in candidates] # Scaling changes presentation only; descending native score has identical order. order = sorted(range(len(candidates)), key=lambda index: (-scores[index], index)) rows.append({'native_scores': scores, 'top3_indices': order[:3], 'top1_tie_count': sum(score == scores[order[0]] for score in scores)}) return rows results, trials, warmups, observations = [], [], [], [] for data in corpora: for mode in ['raw', 'nfkc_casefold']: normalize = (lambda s: s) if mode == 'raw' else (lambda s: unicodedata.normalize('NFKC', s).casefold()) candidates = [normalize(s) for s in data['candidates']] queries = [normalize(q['text']) for q in data['queries']] condition = data['id'] + '-' + mode reference = {} for tool in score_functions: start = time.perf_counter_ns() reference[tool] = evaluate(tool, queries, candidates) elapsed = time.perf_counter_ns() - start warmups.append({'id': condition + '-' + tool, 'elapsed_ns': elapsed}) results.append({'id': condition + '-' + tool, 'condition': condition, 'tool': tool, 'queries': reference[tool], 'output_sha256': digest(reference[tool])}) for repeat in range(5): order = ['rapidfuzz', 'difflib'] if repeat % 2 == 0 else ['difflib', 'rapidfuzz'] for position, tool in enumerate(order): start = time.perf_counter_ns() actual = evaluate(tool, queries, candidates) elapsed = time.perf_counter_ns() - start fingerprint = digest(actual) assert fingerprint == digest(reference[tool]) trials.append({'id': condition + '-' + tool + f'-r{repeat+1}', 'condition': condition, 'tool': tool, 'repeat': repeat + 1, 'position_in_repeat': position + 1, 'elapsed_ns': elapsed, 'seconds': elapsed / 1_000_000_000, 'comparisons': len(queries) * len(candidates), 'output_sha256': fingerprint}) differing = [i for i in range(len(queries)) if reference['rapidfuzz'][i]['top3_indices'][0] != reference['difflib'][i]['top3_indices'][0]] observations.append({'id': condition, 'query_count': len(queries), 'candidate_count': len(candidates), 'top1_agreement_count': len(queries) - len(differing), 'top1_differing_query_indices': differing}) print('completed', condition, flush=True) pairs = [ ('identical', 'alpha notebook', 'alpha notebook'), ('typo', 'rapidfuzz', 'rapidfuz'), ('japanese', '東京都千代田区', '東京都千田区'), ('fullwidth', 'ABC123', 'ABC123'), ('case', 'Notebook', 'notebook'), ('combining', 'café', 'cafe\u0301'), ('transposition', 'abcd', 'acbd'), ('tide-diet', 'tide', 'diet'), ('diet-tide', 'diet', 'tide'), ('empty-empty', '', ''), ('empty-nonempty', '', 'alpha'), ('repeated-300', 'x' + 'a' * 300, 'a' * 300 + 'y'), ] probes = [] for label, a, b in pairs: for mode in ['raw', 'nfkc_casefold']: if mode == 'nfkc_casefold': a, b = [unicodedata.normalize('NFKC', s).casefold() for s in (a, b)] rapid_score = score_functions['rapidfuzz'](a, b) diff_score = score_functions['difflib'](a, b) probes.append({'id': label + '-' + mode, 'a': a, 'b': b, 'rapidfuzz_native_0_100': rapid_score, 'difflib_native_0_1': diff_score, 'difflib_scaled_0_100': diff_score * 100, 'difflib_autojunk_true_0_1': difflib.SequenceMatcher(None, a, b, autojunk=True).ratio()}) raw = { 'schema_version': 1, 'topic_id': 'rapidfuzz-difflib-local-hikaku', 'started_at_utc': began, 'ended_at_utc': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()), 'environment': {'platform': platform.platform(), 'machine': platform.machine(), 'python': sys.version, 'python_implementation': platform.python_implementation(), 'rapidfuzz_version': rapidfuzz.__version__, 'ratio_module': fuzz.ratio.__module__, 'difflib_source_sha256': hashlib.sha256(Path(difflib.__file__).read_bytes()).hexdigest(), 'unicode_version': unicodedata.unidata_version, 'device': 'CPU', 'concurrency': 'single Python process, serial scorer calls; no GPU libraries imported'}, 'conditions': {'seed': SEED, 'repetitions': 5, 'warmup_full_passes_per_tool_condition': 1, 'timing': 'perf_counter_ns; full score vectors + stable top3 sorting + tie counts; hashing excluded', 'rapidfuzz': 'fuzz.ratio(query, candidate, processor=None); native 0..100', 'difflib': 'SequenceMatcher(None, query, candidate, autojunk=False).ratio(); native 0..1', 'normalization': ['raw', 'NFKC then casefold; outside timed region'], 'tie_break': 'higher score then lower candidate index', 'direction': 'query is a, candidate is b; no cache/reused SequenceMatcher', 'excluded': ['imports', 'install/download', 'input generation/normalization', 'file I/O', 'hashing', 'GPU', 'external inference', 'semantic or real-world match quality']}, 'unit_map': {'trials[].seconds': '秒', 'trials[].elapsed_ns': 'ns', 'trials[].comparisons': '件', 'observations[].query_count': '件', 'observations[].candidate_count': '件', 'observations[].top1_agreement_count': '件', 'probes[].rapidfuzz_native_0_100': '点', 'probes[].difflib_native_0_1': '比率', 'probes[].difflib_scaled_0_100': '点', 'probes[].difflib_autojunk_true_0_1': '比率'}, 'inputs_sha256': hashlib.sha256((output / 'inputs.json').read_bytes()).hexdigest(), 'rapidfuzz_import_ns': import_ns, 'warmups': warmups, 'trials': trials, 'results': results, 'observations': observations, 'probes': probes, } dump(output / 'raw.json', raw) for item in observations: line = [item['id'], 'top1 agreement=' + str(item['top1_agreement_count']) + '/16'] for tool in score_functions: ns = [t['elapsed_ns'] for t in trials if t['condition'] == item['id'] and t['tool'] == tool] line.append(tool + ' median_seconds=' + str(statistics.median(ns) / 1e9)) print(' | '.join(line), flush=True) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output-dir', required=True, type=Path) parser.add_argument('--worker', action='store_true') args = parser.parse_args() if args.worker: worker(args.output_dir) return if args.output_dir.exists(): raise SystemExit('Refusing to overwrite existing output directory') args.output_dir.mkdir(parents=True, exist_ok=False) try: # Explicit /private/tmp ensures transient venv/wheels are outside the copied workspace. with tempfile.TemporaryDirectory(prefix='rapidfuzz-cpu-input-', dir='/private/tmp') as transient: tmp = Path(transient) venv.EnvBuilder(with_pip=True, clear=False).create(tmp / 'venv') py = str(tmp / 'venv/bin/python') env = {'PATH': '/usr/bin:/bin:/usr/sbin:/sbin', 'LANG': 'en_US.UTF-8', 'TMPDIR': transient, 'PYTHONDONTWRITEBYTECODE': '1', 'RAPIDFUZZ_IMPLEMENTATION': 'cpp', 'OMP_NUM_THREADS': '1', 'OPENBLAS_NUM_THREADS': '1', 'MKL_NUM_THREADS': '1', 'VECLIB_MAXIMUM_THREADS': '1'} log = [] def run(command): result = subprocess.run(command, env=env, text=True, capture_output=True, check=False) log.append(result.stdout.replace(transient, '')) log.append(result.stderr.replace(transient, '')) if result.returncode: raise RuntimeError('Subprocess failed: ' + ''.join(log)[-4000:]) run([py, '-I', '-m', 'pip', '--isolated', '--disable-pip-version-check', '--no-cache-dir', 'download', '--index-url', 'https://pypi.org/simple', '--no-deps', '--only-binary=:all:', '--dest', str(tmp / 'wheels'), 'RapidFuzz==' + VERSION]) wheels = list((tmp / 'wheels').glob('*.whl')) assert len(wheels) == 1 wheel = wheels[0] request = urllib.request.Request('https://pypi.org/pypi/RapidFuzz/' + VERSION + '/json', headers={'User-Agent': 'local-cpu-measurement/1.0'}) with urllib.request.urlopen(request, timeout=30) as response: metadata = json.load(response) entry = next(row for row in metadata['urls'] if row['filename'] == wheel.name) actual_hash = hashlib.sha256(wheel.read_bytes()).hexdigest() assert actual_hash == entry['digests']['sha256'] run([py, '-I', '-m', 'pip', '--isolated', '--disable-pip-version-check', '--no-cache-dir', 'install', '--no-index', '--no-deps', str(wheel)]) dump(args.output_dir / 'dependency.json', {'rapidfuzz_version': VERSION, 'wheel_filename': wheel.name, 'wheel_url': entry['url'], 'wheel_sha256': actual_hash, 'pypi_declared_sha256': entry['digests']['sha256'], 'wheel_bytes': wheel.stat().st_size, 'temporary_environment_deleted_on_exit': True}) run([py, '-I', str(Path(__file__).resolve()), '--worker', '--output-dir', str(args.output_dir.resolve())]) with (args.output_dir / 'run-log.txt').open('x', encoding='utf-8') as handle: handle.write(''.join(log)) print(''.join(log)[-2000:]) except BaseException: import shutil shutil.rmtree(args.output_dir) raise if __name__ == '__main__': main()