documentationはPythonのitertools製品のメモリ消費量
the actual implementation does not build up intermediate results in memory.
どのようにそれが発電機で可能なことができデカルト製品の機能と言っていますか?誰かが私に2発電機のための限界のあるメモリ消費を伴う の例を表示できますか?
documentationはPythonのitertools製品のメモリ消費量
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()
のメモリ消費量は、入力引数のサイズに直線的であるように思われます。
まあ、それはまた、こう述べています。すべての反復に進ん右端の要素 と走行距離計のような
ネストされたループサイクル。このパターンは、入力のiterablesがソートされている場合、製品 のタプルがソートされた順序で出力されるように、辞書順の の順序付けを作成します。
これはかなりそれが実装にどのように動作するかである(Modules/itertoolsmodule.c
)
は状態オブジェクトである:
typedef struct {
PyObject_HEAD
PyObject *pools; /* tuple of pool tuples */
Py_ssize_t *indices; /* one index per pool */
PyObject *result; /* most recently returned result tuple */
int stopped; /* set to 1 when the product iterator is exhausted */
} productobject;
そして、次の項目は、これを使用する関数product_next
、によって返されます状態と次の状態を生成するために引用符で説明されたアルゴリズム。メモリ要件については、this answerを参照してください。
一般教育の場合は、C拡張子hereの状態で発電機を作成する方法を読むことができます。
[itertools.productでMemoryErrorを取得するのはなぜですか?](http://stackoverflow.com/q/8695422/222914) –