Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add a benchmark for runtime-checkable protocols #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
AlexWaygood committed Apr 25, 2023
commit 04f7bbb9b8085fe8909cb085416e814474fec69f
Original file line number Diff line number Diff line change
Expand Up @@ -149,38 +149,24 @@ def __init__(self):


def bench_protocols(loops: int) -> float:
instances = {
obj: obj()
for obj in globals().values()
if isinstance(obj, type) and not getattr(obj, "_is_protocol", False)
}
protocols = [
HasX, HasManyAttributes, SupportsInt, SupportsManyMethods, SupportsIntAndX
]
instances = [
cls()
for cls in (
Empty, PropertyX, HasIntMethod, HasManyMethods, PropertyXWithInt,
ClassVarX, ClassVarXWithInt, InstanceVarX, ManyInstanceVars,
InstanceVarXWithInt, NominalX, NominalSupportsInt, NominalXWithInt
)
]

t0 = time.perf_counter()

for cls in Empty, PropertyX, InstanceVarX, NominalX:
instance = instances[cls]
for _ in range(loops):
isinstance(instance, HasX)

for cls in Empty, ManyInstanceVars:
instance = instances[cls]
for _ in range(loops):
isinstance(instance, HasManyAttributes)

for cls in Empty, HasIntMethod, NominalSupportsInt:
instance = instances[cls]
for _ in range(loops):
isinstance(instance, SupportsInt)

for cls in Empty, HasManyMethods:
instance = instances[cls]
for _ in range(loops):
isinstance(instance, SupportsManyMethods)

for cls in Empty, PropertyXWithInt, InstanceVarXWithInt, NominalXWithInt:
instance = instances[cls]
for _ in range(loops):
isinstance(instance, SupportsIntAndX)
for _ in range(loops):
for protocol in protocols:
for instance in instances:
isinstance(instance, protocol)

return time.perf_counter() - t0

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.