[Numpy-discussion] avoiding the matrix copy performance hit
Tim Hochberg
tim.hochberg at cox.net
Mon Feb 13 18:02:02 CST 2006
Bill Baxter wrote:
> Is there anyway to get around this timing difference?
> *
> >>> import timeit
> ** >>> t1 = timeit.Timer("a = zeros((1000,1000),'d'); a += 1.;",
> 'from numpy import zeros,mat')
> ** >>> t2 = timeit.Timer("a = mat(zeros((1000,1000),'d')); a +=
> 1.;", 'from numpy import zeros,mat')
> >>> **t1.timeit(100)
> 1.8391627591141742
> >>> t2.timeit(100)
> 3.2988266117713465
>
> *Copying all the data of the input array seems wasteful when the array
> is just going to go out of scope. Or is this not something to be
> concerned about?
You could try using copy=False:
>>> import timeit
>>> t1 = timeit.Timer("a = zeros((1000,1000),'d'); a += 1.;", 'from
numpy import zeros,mat')
>>> t2 = timeit.Timer("a = mat(zeros((1000,1000),'d'), copy=False); a
+= 1.;", 'from numpy import z
eros,mat')
>>> t1.timeit(100)
3.6538127052460578
>>> t2.timeit(100)
3.6567186611706237
I'd also like to point out that your computer appears to be much faster
than mine.
-tim
>
> It seems like a copy-by-reference version of mat() would be useful.
> Really I can't imagine any case when I'd want both a matrix and the
> original version of the array both hanging around as separate copies.
> I can imagine either 1) the array is just a temp and I won't ever need
> it again or 2) temporarily wanting a "matrix view" on the array's data
> to do some linalg, after which I'll go back to using the original (now
> modified) array as an array again.
>
> --bill
More information about the Numpy-discussion
mailing list