| Python Implementation | CPython |
| Python Version | 3.7.13 |
| Operating System | Darwin 20.6.0 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Calls a local function | 2.1805e-07 | 1 |
| Call builtin function | 2.29211e-07 | 1.05118 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize an attrs class | 2.3481e-07 | 1 |
| Initialize a plain class with slots | 2.406e-07 | 1.02466 |
| Initialize a collections.namedtuple class | 2.92533e-07 | 1.24583 |
| Initialize a dataclass | 2.99867e-07 | 1.27706 |
| Initialize a plain class | 3.00333e-07 | 1.27905 |
| Initialize a typing.NamedTuple class | 3.14214e-07 | 1.33817 |
| Initialize a frozen attrs class | 5.072e-07 | 2.16005 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of a frozen attrs class | 1.4529e-07 | 1 |
| Access members of a plain class with slots | 1.4628e-07 | 1.00681 |
| Access members of an attrs class | 1.4742e-07 | 1.01466 |
| Access members of a dataclass | 1.5613e-07 | 1.07461 |
| Access members of a plain class | 1.5879e-07 | 1.09292 |
| Access members of a collections.namedtuple class | 1.98435e-07 | 1.36578 |
| Access members of a typing.NamedTuple class | 4.02e-07 | 2.76688 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 8.25e-07 | 1 |
| Create a dict using keyword arguments | 1.051e-06 | 1.27394 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00680954 | 1 |
| Create a dict from a generator comprehension | 0.0092697 | 1.36128 |
| Create a dict from a list comprehension | 0.0160516 | 2.35723 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00367267 | 1 |
| Use filter on list comprehension | 0.00968373 | 2.6367 |
| Use filter on generator comprehension | 0.0102921 | 2.80234 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.6512e-05 | 1 |
| Access dict values | 9.0522e-05 | 1.94621 |
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.39947e-07 | 1 |
| Get a member by converting to a list first | 0.00071887 | 2995.95 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 4.2586e-07 | 1 |
| Call `set.update` with a set compregension | 4.7906e-07 | 1.12492 |
| Call `set.update` with a list compregension | 5.395e-07 | 1.26685 |
| Call `set.update` with a generator compregension | 5.5364e-07 | 1.30005 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Call `set.update` with a generator compregension | 3.924e-08 | 1 |
| Use `|=` with a set compregension | 4.152e-08 | 1.0581 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.000824155 | 1 |
| Merge sets using set(itertools.chain) | 0.00219736 | 2.6662 |
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.1866e-07 | 1 |
| Split the string using cached slices | 3.1952e-07 | 1.46126 |
| Split the string using simple slices | 3.6374e-07 | 1.6635 |
| Split the string using regex | 6.155e-07 | 2.81487 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0320136 | 1 |
| tuple from generator comprehension | 0.0397482 | 1.2416 |