| Python Implementation | CPython |
| Python Version | 3.8.10 |
| Operating System | Windows 10 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 2.58824e-07 | 1 |
| Calls a local function | 2.6e-07 | 1.00455 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 2.42105e-07 | 1 |
| Initialize a plain class with slots | 2.61111e-07 | 1.0785 |
| Initialize a dataclass | 3.13333e-07 | 1.2942 |
| Initialize a plain class | 3.15e-07 | 1.30109 |
| Initialize a collections.namedtuple class | 3.35714e-07 | 1.38665 |
| Initialize a typing.NamedTuple class | 3.45e-07 | 1.425 |
| Initialize a typing.TypedDict | 4.5e-07 | 1.8587 |
| Initialize a frozen attrs class | 5.35e-07 | 2.20978 |
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 with slots | 1.52e-07 | 1 |
| Access members of a frozen attrs class | 1.53e-07 | 1.00658 |
| Access members of a plain class | 1.57e-07 | 1.03289 |
| Access members of an attrs class | 1.57143e-07 | 1.03383 |
| Access members of a typing.NamedTuple class | 1.60714e-07 | 1.05733 |
| Access members of a collections.namedtuple class | 1.60714e-07 | 1.05733 |
| Access members of a dataclass | 1.62963e-07 | 1.07212 |
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 | 1.1e-06 | 1.57143 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.0112899 | 1 |
| Create a dict from a generator comprehension | 0.0179042 | 1.58586 |
| Create a dict from a list comprehension | 0.0242238 | 2.14562 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.0035745 | 1 |
| Use filter on list comprehension | 0.0120813 | 3.37986 |
| Use filter on generator comprehension | 0.013967 | 3.9074 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 3.46e-05 | 1 |
| Access dict values | 7.46e-05 | 2.15607 |
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.8e-07 | 1 |
| Get a member by converting to a list first | 0.0009149 | 5082.78 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 5.12e-07 | 1 |
| Call `set.update` with a list compregension | 5.5e-07 | 1.07422 |
| Call `set.update` with a set compregension | 5.56e-07 | 1.08594 |
| Call `set.update` with a generator compregension | 5.98e-07 | 1.16797 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a generator compregension | 5e-08 | 1 |
| Use `|=` with a set compregension | 5.8e-08 | 1.16 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.002243 | 1 |
| Merge sets using set(itertools.chain) | 0.0055783 | 2.48698 |
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.18e-07 | 1 |
| Split the string using cached slices | 3.32e-07 | 1.52294 |
| Split the string using simple slices | 3.86e-07 | 1.77064 |
| Split the string using regex | 6.26e-07 | 2.87156 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from generator comprehension | 0.0776124 | 1 |
| tuple from list comprehension | 0.0804491 | 1.03655 |