New submission from Inada Naoki <songofaca...@gmail.com>:
-, |, and ^ of dictkeys are implemented as: PyObject *result = PySet_New(self); // Call set.difference_update, set.update, set.symmetric_difference_update with other. PySet_New(iterable) has optimized step for iterable is dict. But since iterable is dictkeys, PyIter_Next() is called for all elements in the dict. We can pass dict instead of dictkey object to PySet_New. $ ./python -m pyperf timeit -o patched.json -s 'k = dict.fromkeys("abcdefghijklmnopqrstuvwxyz").keys(); s={1,2,3}' -- 'k | {1,2,3}' $ ./python -m pyperf compare_to master.json patched.json Mean +- std dev: [master] 778 ns +- 17 ns -> [patched] 550 ns +- 24 ns: 1.42x faster (-29%) ---------- components: Interpreter Core messages: 355536 nosy: inada.naoki priority: normal severity: normal status: open title: Optimize some set operations in dictkeys object type: performance versions: Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue38613> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com