| Python Implementation | CPython |
| Python Version | 3.7.13 |
| Operating System | Linux 5.13.0-1023-azure |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.5e-07 | 1 |
| Call builtin function | 2.61111e-07 | 1.04444 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 2.4e-07 | 1 |
| Initialize a plain class with slots | 2.5e-07 | 1.04167 |
| Initialize a plain class | 3.0625e-07 | 1.27604 |
| Initialize a dataclass | 3.125e-07 | 1.30208 |
| Initialize a collections.namedtuple class | 3.1875e-07 | 1.32812 |
| Initialize a typing.NamedTuple class | 3.33333e-07 | 1.38889 |
| Initialize a frozen attrs class | 5.4e-07 | 2.25 |
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.41e-07 | 1 |
| Access members of a frozen attrs class | 1.41e-07 | 1 |
| Access members of an attrs class | 1.42e-07 | 1.00709 |
| Access members of a plain class | 1.51e-07 | 1.07092 |
| Access members of a dataclass | 1.51e-07 | 1.07092 |
| Access members of a typing.NamedTuple class | 2.08333e-07 | 1.47754 |
| Access members of a collections.namedtuple class | 2.08333e-07 | 1.47754 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 6e-07 | 1 |
| Create a dict using keyword arguments | 9e-07 | 1.5 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00756709 | 1 |
| Create a dict from a generator comprehension | 0.0101592 | 1.34255 |
| Create a dict from a list comprehension | 0.0196597 | 2.59806 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00312664 | 1 |
| Use filter on list comprehension | 0.00965765 | 3.08883 |
| Use filter on generator comprehension | 0.0107073 | 3.42453 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 3.51e-05 | 1 |
| Access dict values | 6.3101e-05 | 1.79775 |
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 | 2.55e-07 | 1 |
| Get a member by converting to a list first | 0.000678411 | 2660.44 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.6e-07 | 1 |
| Call `set.update` with a set compregension | 5.22e-07 | 1.13478 |
| Call `set.update` with a list compregension | 5.38e-07 | 1.16957 |
| Call `set.update` with a generator compregension | 5.88e-07 | 1.27826 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.2e-08 | 1 |
| Call `set.update` with a generator compregension | 4.4e-08 | 1.04762 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set(itertools.chain) | 0.00233683 | 1 |
| Merge sets using set.union | 0.00254433 | 1.0888 |
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.36e-07 | 1 |
| Split the string using cached slices | 3.38e-07 | 1.4322 |
| Split the string using simple slices | 3.74e-07 | 1.58475 |
| Split the string using regex | 5e-07 | 2.11864 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.03097 | 1 |
| tuple from generator comprehension | 0.0407504 | 1.3158 |