goj*_*omo 5 python dictionary cpython
作为处理将包含数千万或数亿个键的 dict 的优化,我真的非常想预先调整其容量……但似乎没有 Pythonic 方法可以这样做。
使用 Cython 或 C 标注直接调用 CPython 的内部函数(例如dictresize()或_PyDict__NewPresized() )来实现这一点是否可行?
这取决于你所说的实际是什么意思。这当然很简单;你可以打电话_PyDict_NewPresized(howevermany)。哎呀,你甚至可以从 Python 中做到这一点:
>>> import ctypes
>>> import sys
>>> ctypes.pythonapi._PyDict_NewPresized.restype = ctypes.py_object
>>> d = ctypes.pythonapi._PyDict_NewPresized(100)
>>> sys.getsizeof(d)
1676
>>> sys.getsizeof({})
140
>>> len(d)
0
Run Code Online (Sandbox Code Playgroud)
如您所见,dict 是预先确定的,但它没有元素。像这样依赖 CPython 实现细节是否实用取决于您。
| 归档时间: |
|
| 查看次数: |
184 次 |
| 最近记录: |