Prev: recode strings en masse
Next: delete more equal rows
From: Mathew on 27 Mar 2010 20:35 Hi All, I have a simple piece of code to work with two sparse matrices: read each column of one matrix and write values to another sparse matrix. I read from column 1 to end and write from column 1 to end. I may have 1e5 columns to read. The speed is pretty quick to handle the first several thousands and then the speed is slow down significantly. The strange thing is, the speed can be fast if I read the sparse matrix inversely: from column end to 1. Is there any special suggestion on how to access MATLAB's sparse matrix? Thanks, Mathew
From: James Tursa on 28 Mar 2010 02:43 "Mathew " <jhwang2k(a)gmail.com> wrote in message <hom87s$12u$1(a)fred.mathworks.com>... > Hi All, > > I have a simple piece of code to work with two sparse matrices: read each column of one matrix and write values to another sparse matrix. I read from column 1 to end and write from column 1 to end. I may have 1e5 columns to read. > > The speed is pretty quick to handle the first several thousands and then the speed is slow down significantly. The strange thing is, the speed can be fast if I read the sparse matrix inversely: from column end to 1. > > Is there any special suggestion on how to access MATLAB's sparse matrix? > > Thanks, > Mathew When you read from 1 to end without preallocating, MATLAB has to continually reallocate memory and copy memory over and over again. By reading from end to 1, you essentially preallocate some of the sparse matrix and end up saving some of that wasted reallocation and copying, hence it runs faster. If you know the size ahead of time, try the following test: Suppose your matrix is X and you know it will be M x N. Set X(M,N) = 0 ahead of time and then read the rest in from 1 to end. Does this run as fast as not explicitly allocating ahead of time but reading from end to 1? James Tursa
|
Pages: 1 Prev: recode strings en masse Next: delete more equal rows |