| Python Implementation | CPython |
| Python Version | 3.9.13 |
| Operating System | Windows 10 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 2.29412e-07 | 1 |
| Calls a local function | 2.3e-07 | 1.00256 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 2.25e-07 | 1 |
| Initialize a plain class with slots | 2.3e-07 | 1.02222 |
| Initialize a plain class | 2.85e-07 | 1.26667 |
| Initialize a dataclass | 2.85714e-07 | 1.26984 |
| Initialize a collections.namedtuple class | 2.95e-07 | 1.31111 |
| Initialize a typing.TypedDict | 3e-07 | 1.33333 |
| Initialize a typing.NamedTuple class | 3.05e-07 | 1.35556 |
| Initialize a frozen attrs class | 6e-07 | 2.66667 |
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.31e-07 | 1 |
| Access members of a frozen attrs class | 1.34615e-07 | 1.0276 |
| Access members of a typing.NamedTuple class | 1.34615e-07 | 1.0276 |
| Access members of a collections.namedtuple class | 1.36e-07 | 1.03817 |
| Access members of a dataclass | 1.375e-07 | 1.04962 |
| Access members of a plain class | 1.6e-07 | 1.22137 |
| Access members of a plain class with slots | 1.6e-07 | 1.22137 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 8e-07 | 1 |
| Create a dict using keyword arguments | 1e-06 | 1.25 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.0113862 | 1 |
| Create a dict from a generator comprehension | 0.0179438 | 1.57593 |
| Create a dict from a list comprehension | 0.0209503 | 1.83997 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.0037377 | 1 |
| Use filter on list comprehension | 0.0112244 | 3.00302 |
| Use filter on generator comprehension | 0.0115849 | 3.09947 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 2.77e-05 | 1 |
| Access dict values | 6.57e-05 | 2.37184 |
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.0007568 | 4036.27 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a set compregension | 3.82e-07 | 1 |
| Use `|=` with a set compregension | 4.14e-07 | 1.08377 |
| Call `set.update` with a list compregension | 4.3e-07 | 1.12565 |
| Call `set.update` with a generator compregension | 4.86e-07 | 1.27225 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a generator compregension | 6.8e-08 | 1 |
| Use `|=` with a set compregension | 6.8e-08 | 1 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.0022257 | 1 |
| Merge sets using set(itertools.chain) | 0.0048716 | 2.18879 |
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.48e-07 | 1 |
| Split the string using cached slices | 3.08e-07 | 1.24194 |
| Split the string using simple slices | 3.36e-07 | 1.35484 |
| Split the string using regex | 5.4e-07 | 2.17742 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from generator comprehension | 0.0652746 | 1 |
| tuple from list comprehension | 0.0731824 | 1.12115 |