Skip to content

[Performance] Using rich.console.Console() takes multiple seconds #1104

@zachriggle

Description

@zachriggle

Describe the bug
When using rich.console, the startup time of my application increases greatly.

You can see this easily by using hyperfine to benchmark the application.

To Reproduce
First, brew install hyperfine or install Hyperfine in some other way. It takes 80ms for the Python3 interpreter to start, but 1.6s to run rich.console.Console(). This is an unacceptable performance penalty for command-line utilities, and for simple scripts incurs a 20x performance penalty.

Hyperfine Benchmark Results
$ hyperfine "python3 -c 'import sys; sys.exit(0)'"  "python3 -c 'from rich.console import Console; Console()'"
Benchmark #1: python3 -c 'import sys; sys.exit(0)'
  Time (mean ± σ):      83.3 ms ±   3.6 ms    [User: 61.1 ms, System: 19.9 ms]
  Range (min … max):    79.1 ms …  94.3 ms    35 runs

Benchmark #2: python3 -c 'from rich.console import Console; Console()'
  Time (mean ± σ):      1.629 s ±  0.019 s    [User: 992.5 ms, System: 674.1 ms]
  Range (min … max):    1.598 s …  1.660 s    10 runs

Summary
  'python3 -c 'import sys; sys.exit(0)'' ran
   19.57 ± 0.87 times faster than 'python3 -c 'from rich.console import Console; Console()''

Platform

macOS

$ sw_vers
ProductName: macOS
ProductVersion: 11.3
BuildVersion: 20E5172h

Diagnose
I may ask you to cut and paste the output of the following commands. It may save some time if you do it now.

python3 -m rich.diagnose
$ python3 -m rich.diagnose
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=208 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 47                                                            │
│ is_dumb_terminal = False                                                         │
│   is_interactive = True                                                          │
│       is_jupyter = False                                                         │
│      is_terminal = True                                                          │
│   legacy_windows = False                                                         │
│         no_color = False                                                         │
│          options = ConsoleOptions(                                               │
│                        size=ConsoleDimensions(width=208, height=47),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=208,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=208, height=47)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 208                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
python3 -m rich._windows
platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)
pip3 freeze | grep rich
$ pip3 freeze | grep rich
rich==9.13.0

Did I help?

If I was able to resolve your problem, consider sponsoring my work on Rich, or buy me a coffee to say thanks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions