MCPcopy Index your code
hub / github.com/python/cpython / format_comparison

Method format_comparison

Tools/picklebench/memory_dos_impact.py:590–665  ·  view source on GitHub ↗

Format comparison results as readable text.

(
        current_results: Dict[str, Dict[str, Any]],
        baseline_results: Dict[str, Dict[str, Any]]
    )

Source from the content-addressed store, hash-verified

588
589 @staticmethod
590 def format_comparison(
591 current_results: Dict[str, Dict[str, Any]],
592 baseline_results: Dict[str, Dict[str, Any]]
593 ) -> str:
594 """Format comparison results as readable text."""
595 lines = []
596 lines.append("=" * 100)
597 lines.append("Pickle Unpickling Benchmark Comparison")
598 lines.append("=" * 100)
599 lines.append("")
600 lines.append("Legend: Current vs Baseline | % Change (+ is slower/more memory, - is faster/less memory)")
601 lines.append("")
602
603 # Sort size keys numerically
604 for size_key in sorted(current_results.keys(), key=_extract_size_mb):
605 if size_key not in baseline_results:
606 continue
607
608 lines.append(f"\n{size_key} Comparison")
609 lines.append("-" * 100)
610
611 current_tests = current_results[size_key]
612 baseline_tests = baseline_results[size_key]
613
614 for test_name in sorted(current_tests.keys()):
615 if test_name not in baseline_tests:
616 continue
617
618 curr = current_tests[test_name]
619 base = baseline_tests[test_name]
620
621 time_change = Comparator.calculate_change(
622 base['time']['mean'], curr['time']['mean']
623 )
624 mem_change = Comparator.calculate_change(
625 base['memory_peak_mb'], curr['memory_peak_mb']
626 )
627
628 lines.append(f"\n {curr['test_name']}")
629 lines.append(f" Time: {curr['time']['mean']*1000:6.2f}ms vs {base['time']['mean']*1000:6.2f}ms | "
630 f"{time_change:+6.1f}%")
631 lines.append(f" Memory: {curr['memory_peak_mb']:6.2f}MB vs {base['memory_peak_mb']:6.2f}MB | "
632 f"{mem_change:+6.1f}%")
633
634 lines.append("\n" + "=" * 100)
635 lines.append("\nSummary:")
636
637 # Calculate overall statistics
638 time_changes = []
639 mem_changes = []
640
641 for size_key in current_results.keys():
642 if size_key not in baseline_results:
643 continue
644 for test_name in current_results[size_key].keys():
645 if test_name not in baseline_results[size_key]:
646 continue
647 curr = current_results[size_key][test_name]

Callers 1

mainFunction · 0.80

Calls 6

calculate_changeMethod · 0.80
meanMethod · 0.80
medianMethod · 0.80
appendMethod · 0.45
keysMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected