| Python Implementation | PyPy |
| Python Version | 3.9.12 |
| Operating System | Darwin 20.6.0 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 7.779e-10 | 1 |
| Call builtin function | 1.7644e-08 | 22.6816 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a plain class | 8.176e-09 | 1 |
| Initialize a plain class with slots | 8.985e-09 | 1.09895 |
| Initialize an attrs class | 9.8e-09 | 1.19863 |
| Initialize a dataclass | 1.1742e-08 | 1.43615 |
| Initialize a typing.NamedTuple class | 1.2301e-08 | 1.50453 |
| Initialize a collections.namedtuple class | 1.3133e-08 | 1.60629 |
| Initialize a frozen attrs class | 4.21282e-08 | 5.15267 |
| Initialize a typing.TypedDict | 4.03455e-07 | 49.3462 |
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.9298e-08 | 1 |
| Access members of a plain class with slots | 2.1161e-08 | 1.09654 |
| Access members of an attrs class | 2.1963e-08 | 1.1381 |
| Access members of a frozen attrs class | 2.3404e-08 | 1.21277 |
| Access members of a dataclass | 2.4236e-08 | 1.25588 |
| Access members of a typing.NamedTuple class | 2.61337e-08 | 1.35422 |
| Access members of a collections.namedtuple class | 2.76298e-08 | 1.43175 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 3.451e-06 | 1 |
| Create a dict using keyword arguments | 8.907e-06 | 2.58099 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00918855 | 1 |
| Create a dict from a generator comprehension | 0.0160005 | 1.74136 |
| Create a dict from a list comprehension | 0.0161386 | 1.75638 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00157289 | 1 |
| Use filter on list comprehension | 0.0308875 | 19.6374 |
| Use filter on generator comprehension | 0.0544032 | 34.588 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 9.627e-06 | 1 |
| Access dict values | 1.6351e-05 | 1.69845 |
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.0425e-08 | 1 |
| Get a member by converting to a list first | 0.000157253 | 5168.55 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a list compregension | 2.896e-08 | 1 |
| Call `set.update` with a set compregension | 3.038e-08 | 1.04903 |
| Use `|=` with a set compregension | 3.202e-08 | 1.10566 |
| Call `set.update` with a generator compregension | 5.53e-08 | 1.90953 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 3.192e-08 | 1 |
| Call `set.update` with a generator compregension | 4.782e-08 | 1.49812 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.0015844 | 1 |
| Merge sets using set(itertools.chain) | 0.0139688 | 8.8164 |
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 | 3.368e-08 | 1 |
| Split the string using cached slices | 4.924e-08 | 1.462 |
| Split the string using a cached itemgetter | 1.4462e-07 | 4.29394 |
| Split the string using regex | 1.64e-07 | 4.86936 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0524751 | 1 |
| tuple from generator comprehension | 0.0804086 | 1.53232 |