| Python Implementation | CPython |
| Python Version | 3.10.4 |
| Operating System | Linux 5.13.0-1023-azure |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.5e-07 | 1 |
| Call builtin function | 2.5e-07 | 1 |
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 | 2.47059e-07 | 1 |
| Initialize an attrs class | 2.5e-07 | 1.0119 |
| Initialize a typing.TypedDict | 2.85e-07 | 1.15357 |
| Initialize a plain class | 3.15e-07 | 1.275 |
| Initialize a dataclass | 3.16667e-07 | 1.28175 |
| Initialize a collections.namedtuple class | 3.5e-07 | 1.41667 |
| Initialize a typing.NamedTuple class | 3.5e-07 | 1.41667 |
| Initialize a frozen attrs class | 7e-07 | 2.83333 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a frozen attrs class | 1.25e-07 | 1 |
| Access members of a plain class with slots | 1.29167e-07 | 1.03333 |
| Access members of an attrs class | 1.29167e-07 | 1.03333 |
| Access members of a plain class | 1.38e-07 | 1.104 |
| Access members of a dataclass | 1.3901e-07 | 1.11208 |
| Access members of a typing.NamedTuple class | 1.5701e-07 | 1.25608 |
| Access members of a collections.namedtuple class | 1.6401e-07 | 1.31208 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 7e-07 | 1 |
| Create a dict using keyword arguments | 9e-07 | 1.28571 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00897699 | 1 |
| Create a dict from a generator comprehension | 0.0136488 | 1.52042 |
| Create a dict from a list comprehension | 0.0188933 | 2.10464 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00276968 | 1 |
| Use filter on list comprehension | 0.00889498 | 3.21155 |
| Use filter on generator comprehension | 0.0101766 | 3.67427 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 3.2202e-05 | 1 |
| Access dict values | 6.1404e-05 | 1.90684 |
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.9e-07 | 1 |
| Get a member by converting to a list first | 0.000758749 | 3993.42 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.2002e-07 | 1 |
| Call `set.update` with a set compregension | 4.7402e-07 | 1.12857 |
| Call `set.update` with a list compregension | 4.8602e-07 | 1.15714 |
| Call `set.update` with a generator compregension | 4.9802e-07 | 1.18571 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 7e-08 | 1 |
| Call `set.update` with a generator compregension | 7.4e-08 | 1.05714 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.00139899 | 1 |
| Merge sets using set(itertools.chain) | 0.00249286 | 1.7819 |
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.64e-07 | 1 |
| Split the string using cached slices | 3.3602e-07 | 1.2728 |
| Split the string using simple slices | 3.7802e-07 | 1.43189 |
| Split the string using regex | 4.8402e-07 | 1.83341 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0316064 | 1 |
| tuple from generator comprehension | 0.0420737 | 1.33118 |