Skip to content

Instantly share code, notes, and snippets.

@isidentical
Created December 26, 2021 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isidentical/325325766daf7d3588b50704ef7bf064 to your computer and use it in GitHub Desktop.
Save isidentical/325325766daf7d3588b50704ef7bf064 to your computer and use it in GitHub Desktop.
# From https://github.com/ichard26
import tempfile
from pathlib import Path
import pyperf
import black
from blib2to3 import pygram
# First try the relevant function post-refactor in 21.5b1, and fallback
# to old function location.
try:
from black.parsing import lib2to3_parse
except ImportError:
from black import lib2to3_parse
runner = pyperf.Runner()
code = Path("<file>").read_text(encoding="utf8")
with tempfile.TemporaryDirectory(prefix="blackbench-parsing-") as path:
# Block the parser code from using any pre-existing cache.
pygram.initialize(path)
runner.bench_func("-t py39 (<branch>): ", lib2to3_parse, code, set((black.TargetVersion.PY39,)))
runner.bench_func("-t py310 (<branch>): ", lib2to3_parse, code, set((black.TargetVersion.PY310,)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment