#!/usr/bin/env python3 """Recount every median "What speeds up a picture in ComfyUI" prints, from this kit's manifests. python3 recount.py ../manifests # every manifest in the folder python3 recount.py ../manifests/r2-step-ladder.json Standard library only. It reads; it writes nothing. A manifest is a list of rows, one a picture. This script reads four fields of a row: style the column's id (the README's table says which row of the page each id is) subject, seed which picture it is render_seconds ComfyUI's own execution time for the request that drew it, to the millisecond The page's rule, which this script repeats: - a request is one (subject, seed) in a column; the pictures of one batch share one request and one figure, so a batch is counted once; - the batch manifest counts its six batches at seed 202; every other manifest counts the twelve pictures of seeds 202 and 303 (seed 101 was drawn first, read the prompt, and is not counted); - a median of an even count is the mean of its two middle requests, kept exact in decimal (it can end in half a millisecond) and rounded HALF UP to the millisecond, once. For each column it also prints the seed-101 pictures whose prompt was new (every subject but the first, whose prompt the column's warm-up had read): their median less the counted median is what reading a new prompt cost. """ import json import sys from decimal import Decimal, ROUND_HALF_UP from pathlib import Path COUNTED_SEEDS = (202, 303) def median_ms(values): xs = sorted(values) n = len(xs) return Decimal(xs[n // 2]) if n % 2 else (Decimal(xs[n // 2 - 1]) + Decimal(xs[n // 2])) / 2 def seconds(ms): return str((Decimal(ms) / 1000).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)) def to_ms(render_seconds): return int((Decimal(repr(render_seconds)) * 1000).to_integral_exact()) def recount(path): rows = json.loads(Path(path).read_text(encoding="utf-8")) seeds = sorted({r["seed"] for r in rows}) counted_seeds = COUNTED_SEEDS if 303 in seeds else tuple(seeds) # the batch manifest is one seed first_subject = rows[0]["subject"] columns = {} for r in rows: col = columns.setdefault(r["style"], {"counted": {}, "new_prompt": {}}) key = (r["subject"], r["seed"]) # a batch's pictures share a key if r["seed"] in counted_seeds: col["counted"][key] = to_ms(r["render_seconds"]) elif r["seed"] == 101 and r["subject"] != first_subject: col["new_prompt"][key] = to_ms(r["render_seconds"]) print("%s: %d rows, seeds %s, counted %s" % (Path(path).name, len(rows), seeds, list(counted_seeds))) print(" %-10s %9s %10s %9s %9s %s" % ("column", "requests", "median s", "fastest", "slowest", "a new prompt, s")) for style, col in columns.items(): ms = list(col["counted"].values()) m = median_ms(ms) tie = "" if m == m.to_integral_value() else " (exact %s s, a half millisecond rounded up)" % (m / 1000) new = "" if col["new_prompt"]: m101 = median_ms(col["new_prompt"].values()) new = "%s, which is %s more" % (seconds(m101), seconds(m101 - m)) print(" %-10s %9d %10s %9s %9s %s%s" % (style, len(ms), seconds(m), seconds(min(ms)), seconds(max(ms)), new, tie)) print() def main(): if len(sys.argv) < 2: sys.exit(__doc__) for arg in sys.argv[1:]: p = Path(arg) for path in (sorted(p.glob("*.json")) if p.is_dir() else [p]): recount(path) if __name__ == "__main__": main()