| Python Implementation | CPython |
| Python Version | 3.11.0b1 |
| Operating System | Darwin 20.6.0 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 2.21158e-07 | 1 |
| Calls a local function | 2.2885e-07 | 1.03478 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a plain class with slots | 1.84304e-07 | 1 |
| Initialize an attrs class | 1.8612e-07 | 1.00985 |
| Initialize a dataclass | 1.98391e-07 | 1.07643 |
| Initialize a plain class | 1.98773e-07 | 1.0785 |
| Initialize a typing.TypedDict | 2.28579e-07 | 1.24023 |
| Initialize a collections.namedtuple class | 3.003e-07 | 1.62937 |
| Initialize a typing.NamedTuple class | 3.00687e-07 | 1.63147 |
| Initialize a frozen attrs class | 4.9805e-07 | 2.70232 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a plain class | 1.15475e-07 | 1 |
| Access members of a dataclass | 1.16385e-07 | 1.00788 |
| Access members of a plain class with slots | 1.16784e-07 | 1.01133 |
| Access members of an attrs class | 1.20077e-07 | 1.03985 |
| Access members of a frozen attrs class | 1.21026e-07 | 1.04807 |
| Access members of a collections.namedtuple class | 1.66577e-07 | 1.44254 |
| Access members of a typing.NamedTuple class | 1.67821e-07 | 1.45331 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 8.74e-07 | 1 |
| Create a dict using keyword arguments | 9.26e-07 | 1.0595 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00743316 | 1 |
| Create a dict from a list comprehension | 0.0146815 | 1.97513 |
| Create a dict from a generator comprehension | 0.0550191 | 7.40185 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00427727 | 1 |
| Use filter on list comprehension | 0.0082717 | 1.93387 |
| Use filter on generator comprehension | 0.00991415 | 2.31787 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.2415e-05 | 1 |
| Access dict values | 0.000103166 | 2.4323 |
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.69741e-07 | 1 |
| Get a member by converting to a list first | 0.000658153 | 3877.4 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 3.763e-07 | 1 |
| Call `set.update` with a set compregension | 4.2558e-07 | 1.13096 |
| Call `set.update` with a list compregension | 4.3196e-07 | 1.14791 |
| Call `set.update` with a generator compregension | 4.6e-07 | 1.22243 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 6.574e-08 | 1 |
| Call `set.update` with a generator compregension | 6.996e-08 | 1.06419 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set(itertools.chain) | 0.00252345 | 1 |
| Merge sets using set.union | 0.00264077 | 1.04649 |
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.6558e-07 | 1 |
| Split the string using cached slices | 3.5116e-07 | 1.32224 |
| Split the string using simple slices | 4.0296e-07 | 1.51728 |
| Split the string using regex | 5.8688e-07 | 2.2098 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0390657 | 1 |
| tuple from generator comprehension | 0.0513089 | 1.3134 |