gpt4 book ai didi

Python的itertools乘积内存消耗

转载 作者:太空狗 更新时间:2023-10-29 17:31:40 25 4
gpt4 key购买 nike

documentation表示笛卡尔积函数

the actual implementation does not build up intermediate results in memory.

生成器怎么可能呢?有人可以给我举个例子吗2 个生成器的内存消耗有限?

最佳答案

查看模块的源代码,itertools.product() 实际上将每个参数转换为一个元组:

// product_new() in itertoolsmodule.c
for (i=0; i < nargs ; ++i) {
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *pool = PySequence_Tuple(item); //<==== Call tuple(arg)
if (pool == NULL)
goto error;
PyTuple_SET_ITEM(pools, i, pool);
indices[i] = 0;
}

换句话说,itertools.product() 的内存消耗似乎与输入参数的大小成线性关系。

关于Python的itertools乘积内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10720247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com