diff options
author | seberg <sebastian@sipsolutions.net> | 2013-11-26 10:25:00 -0800 |
---|---|---|
committer | seberg <sebastian@sipsolutions.net> | 2013-11-26 10:25:00 -0800 |
commit | f4749b7b2514db9f12978438a2131df981dc14d6 (patch) | |
tree | 847583e7f87558cec6a91090c9fd154103f92e7b /numpy/lib/format.py | |
parent | 78e29a323316642899f8ff85e538b785f0d5e31f (diff) | |
parent | 7a497ffdecceec2d8574674a2b8b04f7927f75d4 (diff) | |
download | numpy-f4749b7b2514db9f12978438a2131df981dc14d6.tar.gz |
Merge pull request #4077 from ogrisel/streaming-numpy-save
Streaming numpy.save to arbitrary file objects
Diffstat (limited to 'numpy/lib/format.py')
-rw-r--r-- | numpy/lib/format.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/numpy/lib/format.py b/numpy/lib/format.py index c411715d8..4ac1427b4 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -399,6 +399,10 @@ def write_array(fp, array, version=(1, 0)): raise ValueError(msg % (version,)) fp.write(magic(*version)) write_array_header_1_0(fp, header_data_from_array_1_0(array)) + + # Set buffer size to 16 MiB to hide the Python loop overhead. + buffersize = max(16 * 1024 ** 2 // array.itemsize, 1) + if array.dtype.hasobject: # We contain Python objects so we cannot write out the data directly. # Instead, we will pickle it out with version 2 of the pickle protocol. @@ -407,14 +411,19 @@ def write_array(fp, array, version=(1, 0)): if isfileobj(fp): array.T.tofile(fp) else: - fp.write(array.T.tostring('C')) + for chunk in numpy.nditer( + array, flags=['external_loop', 'buffered', 'zerosize_ok'], + buffersize=buffersize, order='F'): + fp.write(chunk.tostring('C')) else: if isfileobj(fp): array.tofile(fp) else: - # XXX: We could probably chunk this using something like - # arrayterator. - fp.write(array.tostring('C')) + for chunk in numpy.nditer( + array, flags=['external_loop', 'buffered', 'zerosize_ok'], + buffersize=buffersize, order='C'): + fp.write(chunk.tostring('C')) + def read_array(fp): """ |