| Python Implementation | CPython |
| Python Version | 3.11.0b1 |
| Operating System | Windows 10 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.65e-07 | 1 |
| Call builtin function | 2.71429e-07 | 1.02426 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a plain class with slots | 2.125e-07 | 1 |
| Initialize an attrs class | 2.2e-07 | 1.03529 |
| Initialize a plain class | 2.35e-07 | 1.10588 |
| Initialize a dataclass | 2.35e-07 | 1.10588 |
| Initialize a collections.namedtuple class | 3.5e-07 | 1.64706 |
| Initialize a typing.NamedTuple class | 3.55e-07 | 1.67059 |
| Initialize a typing.TypedDict | 5e-07 | 2.35294 |
| Initialize a frozen attrs class | 5.8e-07 | 2.72941 |
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.34375e-07 | 1 |
| Access members of a frozen attrs class | 1.34375e-07 | 1 |
| Access members of a plain class | 1.35484e-07 | 1.00825 |
| Access members of a dataclass | 1.36667e-07 | 1.01705 |
| Access members of an attrs class | 1.375e-07 | 1.02326 |
| Access members of a typing.NamedTuple class | 1.65385e-07 | 1.23077 |
| Access members of a collections.namedtuple class | 1.65385e-07 | 1.23077 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 9e-07 | 1 |
| Create a dict using keyword arguments | 1e-06 | 1.11111 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.0096562 | 1 |
| Create a dict from a generator comprehension | 0.0166317 | 1.72239 |
| Create a dict from a list comprehension | 0.0246526 | 2.55303 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.003654 | 1 |
| Use filter on list comprehension | 0.0098969 | 2.70851 |
| Use filter on generator comprehension | 0.0119383 | 3.26719 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.15e-05 | 1 |
| Access dict values | 0.0001377 | 3.31807 |
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.1e-07 | 1 |
| Get a member by converting to a list first | 0.0008326 | 3964.76 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.56e-07 | 1 |
| Call `set.update` with a set compregension | 4.98e-07 | 1.09211 |
| Call `set.update` with a list compregension | 5.08e-07 | 1.11404 |
| Call `set.update` with a generator compregension | 5.3e-07 | 1.16228 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 7.6e-08 | 1 |
| Call `set.update` with a generator compregension | 8.6e-08 | 1.13158 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.0020052 | 1 |
| Merge sets using set(itertools.chain) | 0.0045383 | 2.26327 |
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.48e-07 | 1 |
| Split the string using cached slices | 3.6e-07 | 1.45161 |
| Split the string using simple slices | 3.86e-07 | 1.55645 |
| Split the string using regex | 5.98e-07 | 2.41129 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0558101 | 1 |
| tuple from generator comprehension | 0.0738985 | 1.32411 |