| Python Implementation | CPython |
| Python Version | 3.8.12 |
| Operating System | Darwin 20.6.0 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.3425e-07 | 1 |
| Call builtin function | 2.37421e-07 | 1.01354 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 2.49474e-07 | 1 |
| Initialize a plain class with slots | 2.55056e-07 | 1.02237 |
| Initialize a collections.namedtuple class | 2.963e-07 | 1.1877 |
| Initialize a plain class | 3.173e-07 | 1.27188 |
| Initialize a dataclass | 3.21286e-07 | 1.28785 |
| Initialize a typing.NamedTuple class | 3.22429e-07 | 1.29244 |
| Initialize a typing.TypedDict | 3.6785e-07 | 1.4745 |
| Initialize a frozen attrs class | 5.0165e-07 | 2.01083 |
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.6174e-07 | 1 |
| Access members of a frozen attrs class | 1.618e-07 | 1.00037 |
| Access members of an attrs class | 1.6208e-07 | 1.0021 |
| Access members of a plain class | 1.6838e-07 | 1.04105 |
| Access members of a dataclass | 1.6905e-07 | 1.0452 |
| Access members of a collections.namedtuple class | 1.71759e-07 | 1.06194 |
| Access members of a typing.NamedTuple class | 1.718e-07 | 1.0622 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 7.52e-07 | 1 |
| Create a dict using keyword arguments | 9.83e-07 | 1.30718 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00746588 | 1 |
| Create a dict from a generator comprehension | 0.00995857 | 1.33388 |
| Create a dict from a list comprehension | 0.0174849 | 2.34197 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00376101 | 1 |
| Use filter on list comprehension | 0.0102145 | 2.71589 |
| Use filter on generator comprehension | 0.0109234 | 2.90437 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.9605e-05 | 1 |
| Access dict values | 9.7826e-05 | 1.9721 |
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.71909e-07 | 1 |
| Get a member by converting to a list first | 0.000861913 | 5013.77 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.37e-07 | 1 |
| Call `set.update` with a set compregension | 4.8076e-07 | 1.10014 |
| Call `set.update` with a list compregension | 4.8522e-07 | 1.11034 |
| Call `set.update` with a generator compregension | 5.0738e-07 | 1.16105 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a generator compregension | 3.992e-08 | 1 |
| Use `|=` with a set compregension | 4.084e-08 | 1.02305 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.000959003 | 1 |
| Merge sets using set(itertools.chain) | 0.00229318 | 2.39121 |
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.6122e-07 | 1 |
| Split the string using cached slices | 3.843e-07 | 1.47117 |
| Split the string using simple slices | 4.326e-07 | 1.65608 |
| Split the string using regex | 6.2178e-07 | 2.38029 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0360389 | 1 |
| tuple from generator comprehension | 0.0398281 | 1.10514 |