summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorOlivier Grisel <olivier.grisel@ensta.org>2013-11-26 16:11:35 +0100
committerOlivier Grisel <olivier.grisel@ensta.org>2013-11-26 19:21:00 +0100
commit207474ffcf835baa4d8511eff1f021f1b2bf6532 (patch)
treeff8092ac0470f01cb21054feac4a939d70117d33 /numpy/lib
parent78e29a323316642899f8ff85e538b785f0d5e31f (diff)
downloadnumpy-207474ffcf835baa4d8511eff1f021f1b2bf6532.tar.gz
ENH: Use nditer in numpy.save to stream bytes to custom file objects
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/format.py17
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):
"""