Python的itertools产品内存消耗 [英] Python's itertools product memory consumption

查看:112
本文介绍了Python的itertools产品内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档表示笛卡尔积函数

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

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

How can that be possible with generators? Can somebody show me an example with a bounded memory consumption for 2 generators?

推荐答案

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

Looking at the module's source code, itertools.product() actually converts every argument to a tuple:

// 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()的内存消耗在输入参数的大小上似乎是线性的.

In other words, itertools.product()'s memory consumption appears to be linear in the size of the input arguments.

这篇关于Python的itertools产品内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆