| Python Implementation | CPython |
| Python Version | 3.10.4 |
| Operating System | Darwin 20.6.0 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.241e-07 | 1 |
| Call builtin function | 2.24684e-07 | 1.00261 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a typing.TypedDict | 2.438e-07 | 1 |
| Initialize a plain class with slots | 2.54056e-07 | 1.04207 |
| Initialize an attrs class | 2.60944e-07 | 1.07032 |
| Initialize a typing.NamedTuple class | 2.944e-07 | 1.20755 |
| Initialize a collections.namedtuple class | 2.94467e-07 | 1.20782 |
| Initialize a dataclass | 3.14667e-07 | 1.29068 |
| Initialize a plain class | 3.1935e-07 | 1.30989 |
| Initialize a frozen attrs class | 5.58e-07 | 2.28876 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of an attrs class | 1.1513e-07 | 1 |
| Access members of a frozen attrs class | 1.1565e-07 | 1.00452 |
| Access members of a dataclass | 1.2581e-07 | 1.09276 |
| Access members of a plain class | 1.3277e-07 | 1.15322 |
| Access members of a typing.NamedTuple class | 1.6099e-07 | 1.39833 |
| Access members of a collections.namedtuple class | 1.6163e-07 | 1.40389 |
| Access members of a plain class with slots | 2.61e-07 | 2.267 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 8.25e-07 | 1 |
| Create a dict using keyword arguments | 9.31e-07 | 1.12848 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00756874 | 1 |
| Create a dict from a generator comprehension | 0.0112071 | 1.48071 |
| Create a dict from a list comprehension | 0.0141488 | 1.86937 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00338511 | 1 |
| Use filter on list comprehension | 0.00853604 | 2.52165 |
| Use filter on generator comprehension | 0.00917519 | 2.71046 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.7237e-05 | 1 |
| Access dict values | 0.000101963 | 2.15854 |
Get any item from set
In this case we want to get a single element out of a set. It does not matter which element, but we need one.
| Benchmark | Min | Scaled |
|---|---|---|
| Get an item by using iter and next | 1.66857e-07 | 1 |
| Get a member by converting to a list first | 0.000708929 | 4248.72 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.0496e-07 | 1 |
| Call `set.update` with a set compregension | 4.5056e-07 | 1.1126 |
| Call `set.update` with a generator compregension | 4.5458e-07 | 1.12253 |
| Call `set.update` with a list compregension | 4.5976e-07 | 1.13532 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 5.886e-08 | 1 |
| Call `set.update` with a generator compregension | 6.172e-08 | 1.04859 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.00094635 | 1 |
| Merge sets using set(itertools.chain) | 0.00245513 | 2.59431 |
Try and optimiza splitting a 6-character string into a tuple of 3 2-character strings.
This is based on Will McGugan's tweet.
| Benchmark | Min | Scaled |
|---|---|---|
| Split the string using a cached itemgetter | 2.634e-07 | 1 |
| Split the string using cached slices | 3.5226e-07 | 1.33736 |
| Split the string using simple slices | 4.019e-07 | 1.52582 |
| Split the string using regex | 6.0274e-07 | 2.28831 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0293065 | 1 |
| tuple from generator comprehension | 0.0501633 | 1.71168 |