| Python Implementation | CPython |
| Python Version | 3.7.9 |
| Operating System | Windows 10 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.09524e-07 | 1 |
| Call builtin function | 2.14286e-07 | 1.02273 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 1.91667e-07 | 1 |
| Initialize a plain class with slots | 2e-07 | 1.04348 |
| Initialize a plain class | 2.35e-07 | 1.22609 |
| Initialize a collections.namedtuple class | 2.625e-07 | 1.36957 |
| Initialize a typing.NamedTuple class | 2.73333e-07 | 1.42609 |
| Initialize a dataclass | 2.92857e-07 | 1.52795 |
| Initialize a frozen attrs class | 5.55e-07 | 2.89565 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a dataclass | 1.3e-07 | 1 |
| Access members of a plain class | 1.3e-07 | 1 |
| Access members of an attrs class | 1.31e-07 | 1.00769 |
| Access members of a frozen attrs class | 1.31e-07 | 1.00769 |
| Access members of a plain class with slots | 1.32e-07 | 1.01538 |
| Access members of a collections.namedtuple class | 1.72e-07 | 1.32308 |
| Access members of a typing.NamedTuple class | 1.78261e-07 | 1.37124 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 5e-07 | 1 |
| Create a dict using keyword arguments | 9e-07 | 1.8 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.0087957 | 1 |
| Create a dict from a generator comprehension | 0.0146404 | 1.6645 |
| Create a dict from a list comprehension | 0.0207728 | 2.3617 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.0026876 | 1 |
| Use filter on list comprehension | 0.0087997 | 3.27419 |
| Use filter on generator comprehension | 0.0102496 | 3.81366 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 2.7e-05 | 1 |
| Access dict values | 5.63e-05 | 2.08519 |
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 | 2e-07 | 1 |
| Get a member by converting to a list first | 0.0006217 | 3108.5 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4e-07 | 1 |
| Call `set.update` with a list compregension | 4.38e-07 | 1.095 |
| Call `set.update` with a set compregension | 4.42e-07 | 1.105 |
| Call `set.update` with a generator compregension | 4.82e-07 | 1.205 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a generator compregension | 4e-08 | 1 |
| Use `|=` with a set compregension | 4.2e-08 | 1.05 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.0021618 | 1 |
| Merge sets using set(itertools.chain) | 0.0044627 | 2.06434 |
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 | 1.56e-07 | 1 |
| Split the string using cached slices | 2.4e-07 | 1.53846 |
| Split the string using simple slices | 2.72e-07 | 1.74359 |
| Split the string using regex | 5.5e-07 | 3.52564 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from generator comprehension | 0.0556 | 1 |
| tuple from list comprehension | 0.0707394 | 1.27229 |