| Python Implementation | CPython |
| Python Version | 3.8.12 |
| Operating System | Linux 5.13.0-1023-azure |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 2.70529e-07 | 1 |
| Calls a local function | 2.72167e-07 | 1.00605 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 2.57842e-07 | 1 |
| Initialize a plain class with slots | 2.6495e-07 | 1.02757 |
| Initialize a plain class | 3.28571e-07 | 1.27431 |
| Initialize a dataclass | 3.33267e-07 | 1.29252 |
| Initialize a collections.namedtuple class | 3.35714e-07 | 1.30201 |
| Initialize a typing.NamedTuple class | 3.53769e-07 | 1.37204 |
| Initialize a typing.TypedDict | 4.699e-07 | 1.82243 |
| Initialize a frozen attrs class | 5.6995e-07 | 2.21046 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a typing.NamedTuple class | 1.4999e-07 | 1 |
| Access members of a collections.namedtuple class | 1.5e-07 | 1.00007 |
| Access members of an attrs class | 1.6099e-07 | 1.07334 |
| Access members of a frozen attrs class | 1.6099e-07 | 1.07334 |
| Access members of a dataclass | 1.6399e-07 | 1.09334 |
| Access members of a plain class | 1.6399e-07 | 1.09334 |
| Access members of a plain class with slots | 1.6425e-07 | 1.09507 |
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 | 9e-07 | 1.28571 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00726957 | 1 |
| Create a dict from a generator comprehension | 0.00925694 | 1.27338 |
| Create a dict from a list comprehension | 0.0154668 | 2.12761 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00273485 | 1 |
| Use filter on list comprehension | 0.00895694 | 3.27511 |
| Use filter on generator comprehension | 0.0105842 | 3.87012 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 3.0899e-05 | 1 |
| Access dict values | 5.3099e-05 | 1.71847 |
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.79125e-07 | 1 |
| Get a member by converting to a list first | 0.000657989 | 3673.35 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 5.0798e-07 | 1 |
| Call `set.update` with a set compregension | 5.7198e-07 | 1.12599 |
| Call `set.update` with a list compregension | 5.8398e-07 | 1.14961 |
| Call `set.update` with a generator compregension | 6.3798e-07 | 1.25592 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a generator compregension | 3.798e-08 | 1 |
| Use `|=` with a set compregension | 3.998e-08 | 1.05266 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.00137527 | 1 |
| Merge sets using set(itertools.chain) | 0.00198906 | 1.4463 |
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.3598e-07 | 1 |
| Split the string using cached slices | 3.5598e-07 | 1.50852 |
| Split the string using simple slices | 3.9798e-07 | 1.6865 |
| Split the string using regex | 4.9998e-07 | 2.11874 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0270289 | 1 |
| tuple from generator comprehension | 0.0372592 | 1.3785 |