| Python Implementation | PyPy |
| Python Version | 3.9.10 |
| Operating System | Windows 10 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.24e-09 | 1 |
| Call builtin function | 2.32e-08 | 10.3571 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a plain class | 9.8e-09 | 1 |
| Initialize a plain class with slots | 1.05e-08 | 1.07143 |
| Initialize an attrs class | 1.13e-08 | 1.15306 |
| Initialize a dataclass | 1.28e-08 | 1.30612 |
| Initialize a typing.NamedTuple class | 1.31e-08 | 1.33673 |
| Initialize a collections.namedtuple class | 1.39e-08 | 1.41837 |
| Initialize a frozen attrs class | 5e-08 | 5.10204 |
| Initialize a typing.TypedDict | 6.3e-07 | 64.2857 |
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 | 2.52525e-08 | 1 |
| Access members of a plain class with slots | 2.5641e-08 | 1.01538 |
| Access members of an attrs class | 2.60417e-08 | 1.03125 |
| Access members of a frozen attrs class | 2.6738e-08 | 1.05882 |
| Access members of a dataclass | 2.71739e-08 | 1.07609 |
| Access members of a typing.NamedTuple class | 2.90698e-08 | 1.15116 |
| Access members of a collections.namedtuple class | 2.92398e-08 | 1.15789 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 5e-06 | 1 |
| Create a dict using keyword arguments | 9.6e-06 | 1.92 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.0137504 | 1 |
| Create a dict from a generator comprehension | 0.0202212 | 1.47059 |
| Create a dict from a list comprehension | 0.0636158 | 4.62647 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.0015292 | 1 |
| Use filter on list comprehension | 0.019374 | 12.6694 |
| Use filter on generator comprehension | 0.0352507 | 23.0517 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 5.8e-06 | 1 |
| Access dict values | 2.49e-05 | 4.2931 |
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.45324e-08 | 1 |
| Get a member by converting to a list first | 0.0001634 | 4731.79 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a list compregension | 3.2e-08 | 1 |
| Call `set.update` with a set compregension | 3.2e-08 | 1 |
| Use `|=` with a set compregension | 3.2e-08 | 1 |
| Call `set.update` with a generator compregension | 6.6e-08 | 2.0625 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 3.4e-08 | 1 |
| Call `set.update` with a generator compregension | 5.4e-08 | 1.58824 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.0015086 | 1 |
| Merge sets using set(itertools.chain) | 0.0067155 | 4.45148 |
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.4e-08 | 1 |
| Split the string using cached slices | 4.6e-08 | 1.35294 |
| Split the string using a cached itemgetter | 1.62e-07 | 4.76471 |
| Split the string using regex | 1.86e-07 | 5.47059 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0422709 | 1 |
| tuple from generator comprehension | 0.163176 | 3.86026 |