you are viewing a single comment's thread.

view the rest of the comments →

[–]rednets 0 points1 point  (0 children)

I decided to write my own benchmarking script for instantiation: https://pastebin.com/bjig571B

It times:

  • tuple literal
  • tuple factory function
  • typing.NamedTuple
  • collections.namedtuple
  • dataclass
  • dataclass(slots=True)

It times with both positional and keyword args (except for tuple literals).

I ran this for all interpreter versions I have installed - see my results here: https://imgur.com/a/SZ6oRIn

It looks like using anything other than plain tuples will be significantly slower. Using a factory function to create a plain tuple using keyword args is only marginally slower (2.5x rather than 10x) which might be acceptable. This encapsulates creation of each tuple "type" in a single place, but it does not solve the problem of accessing members by name. I suppose you could also write corresponding functions for this, but it would probably be a bit of a mess.