Python's itertools product memory consumption

后端 未结 2 512
礼貌的吻别
礼貌的吻别 2021-02-19 04:55

The documentation says that the cartesian product function

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

How can

2条回答
  •  情深已故
    2021-02-19 05:01

    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;
    }
    

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

提交回复
热议问题