El lun, 17 ene 2022 a las 7:11, Steven D'Aprano (<st...@pearwood.info>) escribió:
> On Mon, Jan 17, 2022 at 11:18:13PM +0900, Inada Naoki wrote: > > On Mon, Jan 17, 2022 at 8:49 PM Steven D'Aprano <st...@pearwood.info> > wrote: > > > > > > On Mon, Jan 17, 2022 at 08:04:50PM +0900, Inada Naoki wrote: > > > > > > > Name lookup is faster than building set in most case. > > > > So I don't think cost to look name up is important at all. > > > > > > But the cost to look up the name is *in addition* to building the set. > > > > > > > I meant it is negligible so we can just ignore it while this discussion. > > On my computer, the name lookup is almost a quarter of the time to build > a set: > > [steve ~]$ python3.10 -m timeit "frozenset" > 10000000 loops, best of 5: 24.4 nsec per loop > [steve ~]$ python3.10 -m timeit "{1, 2, 3, 4, 5}" > 2000000 loops, best of 5: 110 nsec per loop > > and about 10% of the total time: > > [steve ~]$ python3.10 -m timeit "frozenset({1, 2, 3, 4, 5})" > 1000000 loops, best of 5: 237 nsec per loop > > If I use a tuple instead of the set, it is about 12% of the total time: > > [steve ~]$ python3.10 -m timeit "frozenset((1, 2, 3, 4, 5))" > 2000000 loops, best of 5: 193 nsec per loop > > So not negligible. > > That is in the global scope, which will be much slower than a local scope. Global builtins do a hash table lookup; local lookups just follow a pointer. > > > > -- > Steve > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/YL2DW3GDYQZS4HEOIUG6T25HAUL3DGAQ/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/EOAX3UEMBOCNWULBV2K54TUNFMWTT2XM/ Code of Conduct: http://python.org/psf/codeofconduct/