[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: very very very slow computing
From: |
Rob Mahurin |
Subject: |
Re: very very very slow computing |
Date: |
Fri, 6 Feb 2009 12:58:25 -0500 |
On Feb 6, 2009, at 11:04 AM, Depo wrote:
Hi everybody,
I was just trying making some simple operations on a large matrix
(10, 3000) and I faced off o very slow computational time comparing
to the simple operation I was attempting to do. Looking for the
problem I saw that even copying same values to a new matrix was a
computationally huge operation!!!!!!!!!!!!!
That is the code:
clear new_sign_difference; #to ensure it is emty
i=1; k=1;
while (k <= n_rows) #I have used while statement instead of for
because some says it is faster...
while (i <= n_cols)
new_sign_difference(k,i)=sign_difference(k,i) #reassign same
values to a new matrix
disp(i);
disp(k);
i++;
endwhile
k++; i=1;
endwhile
This code does the same thing as the statement
new_sign_difference = sign_difference
which copies the entire matrix at once, very quickly.
If you have some expression you need to do element-by-element, use
"vectorize" to make sure you haven't missed any *,/,^ operators.
If you have some code that doesn't vectorize (hey, it happens),
define matrices for your results before starting so that your loop
doesn't waste time reallocating memory when the matrix grows:
new_sign_difference = zeros(size(sign_difference));
for (i = 1:numel(sign_difference))
new_sign_difference(i) = [something]
the matrix sign_difference has size 10x3'000, so this is a 30'000-
operation loop. It takes up to 10 minutes!
If I only visualize on screen matrix' values it takes 1
second............................., here is the code:
clear new_sign_difference;
i=1; k=1;
while (k <= n_rows) while (i <= n_cols)
sign_difference(k,i) #only to screen, without writing on a new matrix
[...]
Sounds like your problem was repeatedly re-allocating memory.
Allocate, clear, allocate a little more every time turns a 30,000
operation loop to a (30,000)^2 operation loop.
Cheers,
Rob
--
Rob Mahurin
Department of Physics and Astronomy
University of Tennessee 865 207 2594
Knoxville, TN 37996 address@hidden