| Python Implementation | CPython |
| Python Version | 3.9.13 |
| Operating System | Darwin 20.6.0 |
Call global or local functions
| Benchmark | Min | Scaled |
|---|---|---|
| Call builtin function | 2.29167e-07 | 1 |
| Calls a local function | 2.2955e-07 | 1.00167 |
Compare initialization of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Initialize a typing.TypedDict | 2.36375e-07 | 1 |
| Initialize an attrs class | 2.49278e-07 | 1.05459 |
| Initialize a plain class with slots | 2.52471e-07 | 1.06809 |
| Initialize a collections.namedtuple class | 2.86133e-07 | 1.21051 |
| Initialize a typing.NamedTuple class | 2.92267e-07 | 1.23645 |
| Initialize a plain class | 3.023e-07 | 1.2789 |
| Initialize a dataclass | 3.07357e-07 | 1.30029 |
| Initialize a frozen attrs class | 4.7685e-07 | 2.01735 |
Compare member access of different class types. The idea is to compare attrs, dataclasses, and plain classes
| Benchmark | Min | Scaled |
|---|---|---|
| Access members of an attrs class | 1.6977e-07 | 1 |
| Access members of a typing.NamedTuple class | 1.6987e-07 | 1.00059 |
| Access members of a frozen attrs class | 1.723e-07 | 1.0149 |
| Access members of a collections.namedtuple class | 1.72346e-07 | 1.01517 |
| Access members of a plain class with slots | 1.748e-07 | 1.02963 |
| Access members of a plain class | 1.7659e-07 | 1.04017 |
| Access members of a dataclass | 1.7722e-07 | 1.04388 |
Compare different methods of creating a dict literal
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict using a literal | 7.7e-07 | 1 |
| Create a dict using keyword arguments | 9.7e-07 | 1.25974 |
Compare different comprehensions for dict creation
| Benchmark | Min | Scaled |
|---|---|---|
| Create a dict comprehension | 0.00773794 | 1 |
| Create a dict from a generator comprehension | 0.00976704 | 1.26223 |
| Create a dict from a list comprehension | 0.016067 | 2.0764 |
| Benchmark | Min | Scaled |
|---|---|---|
| Filter in the comprehension | 0.00400678 | 1 |
| Use filter on list comprehension | 0.00980215 | 2.44639 |
| Use filter on generator comprehension | 0.00997454 | 2.48942 |
Compare using a list or a dict to index-based lookups.
| Benchmark | Min | Scaled |
|---|---|---|
| Access list values | 4.8811e-05 | 1 |
| Access dict values | 0.000101023 | 2.06968 |
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.8181e-07 | 1 |
| Get a member by converting to a list first | 0.000724113 | 3982.81 |
Update sets in different ways when generating the additional set
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 3.896e-07 | 1 |
| Call `set.update` with a set compregension | 4.3408e-07 | 1.11417 |
| Call `set.update` with a generator compregension | 4.5566e-07 | 1.16956 |
| Call `set.update` with a list compregension | 4.6826e-07 | 1.2019 |
Compare set.update and set |=
| Benchmark | Min | Scaled |
|---|---|---|
| Use `|=` with a set compregension | 6.16e-08 | 1 |
| Call `set.update` with a generator compregension | 6.222e-08 | 1.01006 |
Test the performance of different ways to unify multiple sets.
| Benchmark | Min | Scaled |
|---|---|---|
| Merge sets using set.union | 0.000822973 | 1 |
| Merge sets using set(itertools.chain) | 0.0022487 | 2.73242 |
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.6992e-07 | 1 |
| Split the string using cached slices | 3.802e-07 | 1.40857 |
| Split the string using simple slices | 4.2882e-07 | 1.58869 |
| Split the string using regex | 5.7746e-07 | 2.13937 |
Compare various ways to write "tuple comprehensions"
| Benchmark | Min | Scaled |
|---|---|---|
| tuple from list comprehension | 0.0351177 | 1 |
| tuple from generator comprehension | 0.0420049 | 1.19612 |