| Python Implementation | CPython |
| Python Version | 3.10.4 |
| Operating System | Windows 10 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 2.3e-07 | 1 |
| Calls a local function | 3e-07 | 1.30435 |
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.31579e-07 | 1 |
| Initialize an attrs class | 2.33333e-07 | 1.00758 |
| Initialize a dataclass | 2.82353e-07 | 1.21925 |
| Initialize a plain class | 2.85e-07 | 1.23068 |
| Initialize a typing.TypedDict | 3e-07 | 1.29545 |
| Initialize a typing.NamedTuple class | 3.0625e-07 | 1.32244 |
| Initialize a collections.namedtuple class | 3.06667e-07 | 1.32424 |
| Initialize a frozen attrs class | 5.1e-07 | 2.20227 |
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.32e-07 | 1 |
| Access members of a frozen attrs class | 1.32e-07 | 1 |
| Access members of a plain class with slots | 1.32e-07 | 1 |
| Access members of a dataclass | 1.39e-07 | 1.05303 |
| Access members of a plain class | 1.4e-07 | 1.06061 |
| Access members of a typing.NamedTuple class | 1.52e-07 | 1.15152 |
| Access members of a collections.namedtuple class | 1.56e-07 | 1.18182 |
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 | 8e-07 | 1.14286 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.0093533 | 1 |
| Create a dict from a generator comprehension | 0.0161143 | 1.72285 |
| Create a dict from a list comprehension | 0.0204863 | 2.19028 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.0041673 | 1 |
| Use filter on list comprehension | 0.0104844 | 2.51587 |
| Use filter on generator comprehension | 0.0123508 | 2.96374 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 3.42e-05 | 1 |
| Access dict values | 6.74e-05 | 1.97076 |
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.875e-07 | 1 |
| Get a member by converting to a list first | 0.000765 | 4080 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.5e-07 | 1 |
| Call `set.update` with a list compregension | 4.74e-07 | 1.05333 |
| Call `set.update` with a set compregension | 4.96e-07 | 1.10222 |
| Call `set.update` with a generator compregension | 5.02e-07 | 1.11556 |
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.0022572 | 1 |
| Merge sets using set(itertools.chain) | 0.004524 | 2.00425 |
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.08e-07 | 1 |
| Split the string using cached slices | 2.9e-07 | 1.39423 |
| Split the string using simple slices | 3.32e-07 | 1.59615 |
| Split the string using regex | 5.76e-07 | 2.76923 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from generator comprehension | 0.070273 | 1 |
| tuple from list comprehension | 0.0725071 | 1.03179 |