Skip to content
Merged
Changes from all commits
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
42 changes: 42 additions & 0 deletions bench/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from __future__ import annotations

import functools
import time

import pytest

import attrs
Expand Down Expand Up @@ -172,3 +175,42 @@ def test_astuple_atomic():

for _ in range(ROUNDS):
at(c)


class TestCachedProperties:
@attrs.define
class Slotted:
x: int = 0

@functools.cached_property
def cached(self):
time.sleep(0.1)
return 42

@attrs.define(slots=False)
class Unslotted:
x: int = 0

@functools.cached_property
def cached(self):
time.sleep(0.1)
return 42

def test_first_access(self):
"""
Benchmark first access to a cached property (computation + storage).
"""
for _ in range(ROUNDS):
c = self.Slotted(42)
_ = c.cached

def test_repeated_access(self):
"""
Benchmark repeated access to a cached property (should use stored
value).
"""
c = self.Slotted(42)
_ = c.cached # Prime the cache

for _ in range(ROUNDS):
_ = c.cached
Loading