What's the public API alternative to _PyObject_GC_IS_TRACKED()?

Chris Angelico rosuav at gmail.com
Wed Dec 29 04:27:53 EST 2021


On Wed, Dec 29, 2021 at 8:07 PM Dieter Maurer <dieter at handshake.de> wrote:
>
> Marco Sulla wrote at 2021-12-29 09:29 +0100:
> >On second thought, I think I'll do this for the pure py version. But I
> >will definitely not do this for the C extension
>
> Are you sure you need to implement your type in C at all?
>
> I made a small `timeit` test:
> ```
> >>> class cd(dict): pass
> ...
> >>> timeit("d[1]", "d={1:1}", globals=globals())
> 0.02474160000019765
> >>> timeit("d[1]", "d=cd({1:1})", globals=globals())
> 0.08281239100051607
> ```
> This means that for the above trivial case, access is 3.5 times slower
> (the difference is smaller for more complex cases when hashing
> becomes more expensive) but it is still only 83 ns per access.
> Thus, if your application is not highly dominated by dict accesses,
> the overall difference will not be large.

You forgot slots. The difference can be even smaller.

>>> class cd(dict): __slots__ = ()
...
>>> timeit("d[1]", "d={1:1}", globals=globals())
0.02511977031826973
>>> timeit("d[1]", "d=cd({1:1})", globals=globals())
0.0333079993724823

ChrisA


More information about the Python-list mailing list