[Numpy-discussion] fromstring, tostring slow?
Stefan van der Walt
Tue Feb 13 14:03:26 CST 2007
On Tue, Feb 13, 2007 at 11:42:35AM -0800, Mark Janikas wrote:
> I am finding that directly packing numpy arrays into binary using the tostring
> and fromstring methods do not provide a speed improvement over writing the same
> arrays to ascii files. Obviously, the size of the resulting files is far
> smaller, but I was hoping to get an improvement in the speed of writing. I got
> that speed improvement using the struct module directly, or by using generic
> python arrays. Let me further describe my methodological issue as it may
> directly relate to any solution you might have.
Can you post a benchmark code snippet to demonstrate your results?
Here, using 1.0.2.dev3545, I see:
In : x = N.random.random(100)
In : timeit f = file('/tmp/blah.dat','w'); f.write(str(x))
100 loops, best of 3: 1.77 ms per loop
In : timeit f = file('/tmp/blah','w'); x.tofile(f)
10000 loops, best of 3: 100 µs per loop
(I see the same results for heterogeneous arrays)
More information about the Numpy-discussion