| Python Implementation | PyPy |
| Python Version | 3.8.13 |
| Operating System | Linux 5.13.0-1023-azure |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 8.099e-10 | 1 |
| Call builtin function | 1.6499e-08 | 20.3717 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a plain class | 6.899e-09 | 1 |
| Initialize a plain class with slots | 7.699e-09 | 1.11596 |
| Initialize an attrs class | 8.199e-09 | 1.18843 |
| Initialize a dataclass | 9.699e-09 | 1.40586 |
| Initialize a typing.NamedTuple class | 1.0499e-08 | 1.52181 |
| Initialize a collections.namedtuple class | 1.0499e-08 | 1.52181 |
| Initialize a frozen attrs class | 2.58011e-08 | 3.73983 |
| Initialize a typing.TypedDict | 4.49917e-07 | 65.2148 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a plain class | 1.8899e-08 | 1 |
| Access members of a plain class with slots | 1.9199e-08 | 1.01587 |
| Access members of an attrs class | 2e-08 | 1.05826 |
| Access members of a frozen attrs class | 2.0799e-08 | 1.10053 |
| Access members of a dataclass | 2.16e-08 | 1.14292 |
| Access members of a typing.NamedTuple class | 2.4199e-08 | 1.28044 |
| Access members of a collections.namedtuple class | 2.53756e-08 | 1.3427 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 2.1e-06 | 1 |
| Create a dict using keyword arguments | 3.9e-06 | 1.85714 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict from a generator comprehension | 0.013188 | 1 |
| Create a dict comprehension | 0.0135536 | 1.02772 |
| Create a dict from a list comprehension | 0.0150281 | 1.13953 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.000893386 | 1 |
| Use filter on list comprehension | 0.0150183 | 16.8106 |
| Use filter on generator comprehension | 0.0272566 | 30.5093 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 2.6495e-06 | 1 |
| Access dict values | 1.3999e-05 | 5.28364 |
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 | 3.0858e-08 | 1 |
| Get a member by converting to a list first | 0.000161898 | 5246.54 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a list compregension | 2.598e-08 | 1 |
| Call `set.update` with a set compregension | 2.798e-08 | 1.07698 |
| Use `|=` with a set compregension | 2.8e-08 | 1.07775 |
| Call `set.update` with a generator compregension | 5.598e-08 | 2.15473 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 2.598e-08 | 1 |
| Call `set.update` with a generator compregension | 4.198e-08 | 1.61586 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.000502594 | 1 |
| Merge sets using set(itertools.chain) | 0.00310586 | 6.17966 |
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 simple slices | 2.998e-08 | 1 |
| Split the string using cached slices | 4.198e-08 | 1.40027 |
| Split the string using a cached itemgetter | 1.26e-07 | 4.2028 |
| Split the string using regex | 1.32e-07 | 4.40294 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0333615 | 1 |
| tuple from generator comprehension | 0.0737987 | 2.21209 |