help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

help requested with extremely slow for loop?


From: Joachim De Beule
Subject: help requested with extremely slow for loop?
Date: Wed, 24 Mar 2010 11:22:29 +0100
User-agent: KMail/1.11.2 (Linux/2.6.28-15-generic; KDE/4.2.2; x86_64; ; )

Hi all,

I have some Octave code that is extremely slow code and I don't really 
understand why. The fact that for loops are generally 'rather slow' does not 
explain it I think.

I have two sparse arrays x and y. 


        octave> size(x)
        ans =  1   15071213
        octave> nnz(x)
        ans =  213
        octave> size(y)
        ans =  1   15071213
        octave> nnz(y)
        ans =  2561252
        octave> size(find(x & y))
        ans =  1   75

As can be seen, the x and y arrays only share 75 non-zero values. Still, 
looping over them as in the following code takes almost 16 seconds, which 
really is unacceptable (since I actually have thousands of x's for which I 
need to do this computation):

        
        octave> idxs = find(x & y);
        octace>
        tic
        for j = idxs
                x(y(j)) += x(j);
                x(j) = 0;
        endfor
        toc

        octave> Elapsed time is 15.96 seconds.

Does anybody have an idea what is going on here or have suggestions about how 
to do this differently? Basically, y(j) holds the new j' that should get the 
value of x(j), after which x(j) itself needs to be set to zero...

Thanks!

Joachim.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]