code: def test_compare(self):
      # Simulate several approaches.
      costs = (
          # overhead_optimized_fn()
          (1e-6, 1e-9),
          # compute_optimized_fn()
          (3e-6, 5e-10),
          # special_case_fn()  [square inputs only]
          (1e-6, 4e-10),
      )
  
      sizes = (
          (16, 16),
          (16, 128),
          (128, 128),
          (4096, 1024),
          (2048, 2048),
      )
  
      # overhead_optimized_fn()
      class _MockTimer_0(self._MockTimer):
          _function_costs = tuple(
              (f"fn({i}, {j})", costs[0][0] + costs[0][1] * i * j)
              for i, j in sizes
          )
  
      class MockTimer_0(benchmark_utils.Timer):
          _timer_cls = _MockTimer_0
  
      # compute_optimized_fn()
      class _MockTimer_1(self._MockTimer):
          _function_costs = tuple(
              (f"fn({i}, {j})", costs[1][0] + costs[1][1] * i * j)
              for i, j in sizes
          )
  
      class MockTimer_1(benchmark_utils.Timer):
          _timer_cls = _MockTimer_1
  
      # special_case_fn()
      class _MockTimer_2(self._MockTimer):
          _function_costs = tuple(
              (f"fn({i}, {j})", costs[2][0] + costs[2][1] * i * j)
              for i, j in sizes
              if i == j
          )
  
      class MockTimer_2(benchmark_utils.Timer):
          _timer_cls = _MockTimer_2
  
      results = []
      for i, j in sizes:
          results.append(
              MockTimer_0(
                  f"fn({i}, {j})",
                  label="fn",
                  description=f"({i}, {j})",
                  sub_label="overhead_optimized",
              ).blocked_autorange(min_run_time=10)
          )
  
          results.append(
              MockTimer_1(
                  f"fn({i}, {j})",
                  label="fn",
                  description=f"({i}, {j})",
                  sub_label="compute_optimized",
              ).blocked_autorange(min_run_time=10)
          )
  
          if i == j:
              results.append(
                  MockTimer_2(
                      f"fn({i}, {j})",
                      label="fn",
                      description=f"({i}, {j})",
                      sub_label="special_case (square)",
                  ).blocked_autorange(min_run_time=10)
              )
  
      def rstrip_lines(s: str) -> str:
          # VSCode will rstrip the `expected` string literal whether you like
          # it or not. So we have to rstrip the compare table as well.
          return "\n".join(
              [i.rstrip() for i in s.splitlines(keepends=False)]
          )
  
      compare = benchmark_utils.Compare(results)
      self.regularizeAndAssertExpectedInline(
          rstrip_lines(str(compare).strip()),
          """\
              [------------------------------------------------- fn ------------------------------------------------]
                                           |  (16, 16)  |  (16, 128)  |  (128, 128)  |  (4096, 1024)  |  (2048, 2048)
              1 threads: --------------------------------------------------------------------------------------------
                    overhead_optimized     |    1.3     |     3.0     |     17.4     |     4174.4     |     4174.4
                    compute_optimized      |    3.1     |     4.0     |     11.2     |     2099.3     |     2099.3
                    special_case (square)  |    1.1     |             |      7.5     |                |     1674.7
  
              Times are in microseconds (us).""",
      )
  
      compare.trim_significant_figures()
      self.regularizeAndAssertExpectedInline(
          rstrip_lines(str(compare).strip()),
          """\
              [------------------------------------------------- fn ------------------------------------------------]
                                           |  (16, 16)  |  (16, 128)  |  (128, 128)  |  (4096, 1024)  |  (2048, 2048)
              1 threads: --------------------------------------------------------------------------------------------
                    overhead_optimized     |     1      |     3.0     |      17      |      4200      |      4200
                    compute_optimized      |     3      |     4.0     |      11      |      2100      |      2100
                    special_case (square)  |     1      |             |       8      |                |      1700
  
              Times are in microseconds (us).""",
      )
  
      compare.colorize()
      columnwise_colored_actual = rstrip_lines(str(compare).strip())
      columnwise_colored_expected = textwrap.dedent(
          """\
              [------------------------------------------------- fn ------------------------------------------------]
                                           |  (16, 16)  |  (16, 128)  |  (128, 128)  |  (4096, 1024)  |  (2048, 2048)
              1 threads: --------------------------------------------------------------------------------------------
                    overhead_optimized     |     1      |  \x1b[92m\x1b[1m   3.0   \x1b[0m\x1b[0m  |  \x1b[2m\x1b[91m    17    \x1b[0m\x1b[0m  |      4200      |  \x1b[2m\x1b[91m    4200    \x1b[0m\x1b[0m
                    compute_optimized      |  \x1b[2m\x1b[91m   3    \x1b[0m\x1b[0m  |     4.0     |      11      |  \x1b[92m\x1b[1m    2100    \x1b[0m\x1b[0m  |      2100
                    special_case (square)  |  \x1b[92m\x1b[1m   1    \x1b[0m\x1b[0m  |             |  \x1b[92m\x1b[1m     8    \x1b[0m\x1b[0m  |                |  \x1b[92m\x1b[1m    1700    \x1b[0m\x1b[0m
  
              Times are in microseconds (us)."""  # noqa: B950
      )
  
      compare.colorize(rowwise=True)
      rowwise_colored_actual = rstrip_lines(str(compare).strip())
      rowwise_colored_expected = textwrap.dedent(
          """\
              [------------------------------------------------- fn ------------------------------------------------]
                                           |  (16, 16)  |  (16, 128)  |  (128, 128)  |  (4096, 1024)  |  (2048, 2048)
              1 threads: --------------------------------------------------------------------------------------------
                    overhead_optimized     |  \x1b[92m\x1b[1m   1    \x1b[0m\x1b[0m  |  \x1b[2m\x1b[91m   3.0   \x1b[0m\x1b[0m  |  \x1b[31m\x1b[1m    17    \x1b[0m\x1b[0m  |  \x1b[31m\x1b[1m    4200    \x1b[0m\x1b[0m  |  \x1b[31m\x1b[1m    4200    \x1b[0m\x1b[0m
                    compute_optimized      |  \x1b[92m\x1b[1m   3    \x1b[0m\x1b[0m  |     4.0     |  \x1b[2m\x1b[91m    11    \x1b[0m\x1b[0m  |  \x1b[31m\x1b[1m    2100    \x1b[0m\x1b[0m  |  \x1b[31m\x1b[1m    2100    \x1b[0m\x1b[0m
                    special_case (square)  |  \x1b[92m\x1b[1m   1    \x1b[0m\x1b[0m  |             |  \x1b[31m\x1b[1m     8    \x1b[0m\x1b[0m  |                |  \x1b[31m\x1b[1m    1700    \x1b[0m\x1b[0m
  
              Times are in microseconds (us)."""  # noqa: B950
      )
  
      def print_new_expected(s: str) -> None:
          print(f'{"":>12}"""\\', end="")
          for l in s.splitlines(keepends=False):
              print(
                  "\n" + textwrap.indent(repr(l)[1:-1], " " * 12),
                  end="",
              )
          print('"""\n')
  
      if expecttest.ACCEPT:
          # expecttest does not currently support non-printable characters,
          # so these two entries have to be updated manually.
          if columnwise_colored_actual != columnwise_colored_expected:
              print("New columnwise coloring:\n")
              print_new_expected(columnwise_colored_actual)
  
          if rowwise_colored_actual != rowwise_colored_expected:
              print("New rowwise coloring:\n")
              print_new_expected(rowwise_colored_actual)
  
      self.assertEqual(
          columnwise_colored_actual, columnwise_colored_expected
      )
      self.assertEqual(rowwise_colored_actual, rowwise_colored_expected)
  
imports: import collections
  import json
  import os
  import re
  import textwrap
  import timeit
  from typing import Any, List, Tuple
  import unittest
  import torch
  import torch.utils.benchmark
  from torch.testing._internal.common_utils import TestCase, run_tests, IS_SANDCASTLE, IS_WINDOWS, slowTest, TEST_WITH_ASAN
  import expecttest
  import numpy
label_desc: Use image node_id 9 for calling the TestBenchmarkUtils obj's underlying member method code with example usage: obj.test_compare() and returns: str