| Python Implementation | CPython |
| Python Version | 3.9.12 |
| Operating System | Linux 5.13.0-1023-azure |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 3.1e-07 | 1 |
| Calls a local function | 3.2e-07 | 1.03226 |
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.85e-07 | 1 |
| Initialize an attrs class | 2.9e-07 | 1.01754 |
| Initialize a typing.TypedDict | 3.38462e-07 | 1.18758 |
| Initialize a typing.NamedTuple class | 3.65e-07 | 1.2807 |
| Initialize a collections.namedtuple class | 3.7e-07 | 1.29825 |
| Initialize a dataclass | 3.9e-07 | 1.36842 |
| Initialize a plain class | 5e-07 | 1.75439 |
| Initialize a frozen attrs class | 6.15e-07 | 2.15789 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a collections.namedtuple class | 1.85e-07 | 1 |
| Access members of a plain class | 1.95238e-07 | 1.05534 |
| Access members of a plain class with slots | 1.95455e-07 | 1.05651 |
| Access members of a typing.NamedTuple class | 1.95455e-07 | 1.05651 |
| Access members of a frozen attrs class | 2e-07 | 1.08108 |
| Access members of an attrs class | 2.04545e-07 | 1.10565 |
| Access members of a dataclass | 2.04762e-07 | 1.10682 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using keyword arguments | 1e-06 | 1 |
| Create a dict using a literal | 1.001e-06 | 1.001 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00947041 | 1 |
| Create a dict from a generator comprehension | 0.013422 | 1.41725 |
| Create a dict from a list comprehension | 0.021192 | 2.23771 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00325844 | 1 |
| Use filter on list comprehension | 0.010254 | 3.14691 |
| Use filter on generator comprehension | 0.0121854 | 3.73966 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.4801e-05 | 1 |
| Access dict values | 0.000110501 | 2.46649 |
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 | 2.23529e-07 | 1 |
| Get a member by converting to a list first | 0.000723808 | 3238.09 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.7e-07 | 1 |
| Call `set.update` with a set compregension | 5.3602e-07 | 1.14047 |
| Call `set.update` with a list compregension | 5.52e-07 | 1.17447 |
| Call `set.update` with a generator compregension | 6.04e-07 | 1.28511 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 7e-08 | 1 |
| Call `set.update` with a generator compregension | 7.6e-08 | 1.08571 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set(itertools.chain) | 0.00254003 | 1 |
| Merge sets using set.union | 0.00269973 | 1.06287 |
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.84e-07 | 1 |
| Split the string using cached slices | 4.16e-07 | 1.46479 |
| Split the string using simple slices | 4.8e-07 | 1.69014 |
| Split the string using regex | 5.12e-07 | 1.80282 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0367906 | 1 |
| tuple from generator comprehension | 0.0456681 | 1.2413 |