0

Can someone explain to me please why

B = diag(-2*ones(m,1),0) + diag(ones(m-1,1),-1) + diag(ones(m-1,1),1) 

takes 1,22 s. While

A = diag(-2*ones(1,n)) + diag(ones(1,n-1),1) + diag(ones(1,n-1),-1)

takes ~ an hour, for the same n=m=10000

5
  • I'd hazard a guess that ones(m,1) is nice and local cache friendly consecutive memory accesses and that ones(1,n) has significant extra admin overheads. I'm a bit surprised the difference in speed is quite so large. Commented Apr 26 at 12:56
  • 4
    When you say “MATLAB / OCTAVE”, I presume you mean just Octave. People that question MATLAB’s speed don’t tent to mention Octave. These are two very different programs, even if their languages are very similar. Especially on speed, MATLAB and Octave have completely different characteristics. And these performance characteristics change across versions too. So please state which one you use, and what your version number is. Commented Apr 26 at 13:23
  • 1
    In addition to what Cris said, I don't see that much of a difference in Matlab R2017b or in Octave 4.2.2 Commented Apr 26 at 18:28
  • No significant difference in online MATLAB R2024b either. Are sparse matrices involved, or is everything full? Storage for sparse column vectors vs row vectors is a bit different and can affect performance in some situations. Commented Apr 28 at 17:36
  • what version of Octave are you running? in a recent stable build (based on 10.1, soon to be a 10.2 release available from nightly.octave.org ) I'm getting nearly identical times. just tested, i get the same results on version 9.1.0 Commented Apr 30 at 15:23

1 Answer 1

1

You didn't specify which version of Octave (and/or Matlab?) you are using. It may be that older versions failed to perform internal optimizations to account for column-ordered operations being more memory and speed friendly. Recent versions of both programs show nearly identical run times for the commands you provided.

Testing on recent versions of Octave:

Octave 9.1.0:

----------------------------------------------------------------------
GNU Octave Version: 9.1.0 (hg id: d0c18b1446df)
GNU Octave License: GNU General Public License
Operating System: MINGW32_NT-6.2 Windows 6.2  x86_64
----------------------------------------------------------------------
>> n = m = 10000;
>> tic; B = diag(-2*ones(m,1),0) + diag(ones(m-1,1),-1) + diag(ones(m-1,1),1); toc
Elapsed time is 0.989229 seconds.
>> tic; A = diag(-2*ones(1,n)) + diag(ones(1,n-1),1) + diag(ones(1,n-1),-1); toc
Elapsed time is 0.981692 seconds.

Octave 10.1.0:

----------------------------------------------------------------------
GNU Octave Version: 10.1.0 (hg id: 417c47651ed5)
GNU Octave License: GNU General Public License
Operating System: MINGW32_NT-6.2 Windows 6.2  x86_64
----------------------------------------------------------------------

>> n = m = 10000;
>> tic; B = diag(-2*ones(m,1),0) + diag(ones(m-1,1),-1) + diag(ones(m-1,1),1); toc
Elapsed time is 1.24033 seconds.
>> tic; A = diag(-2*ones(1,n)) + diag(ones(1,n-1),1) + diag(ones(1,n-1),-1); toc
Elapsed time is 1.29325 seconds.

and a recent nightly build, soon to become 10.2.0:

----------------------------------------------------------------------
GNU Octave Version: 10.1.1 (hg id: 34e0522499ae)
GNU Octave License: GNU General Public License
Operating System: MINGW32_NT-6.2 Windows 6.2  x86_64
----------------------------------------------------------------------
>> n = m = 10000;
>> tic;B = diag(-2*ones(m,1),0) + diag(ones(m-1,1),-1) + diag(ones(m-1,1),1); toc
Elapsed time is 1.06365 seconds.
>> tic;A = diag(-2*ones(1,n)) + diag(ones(1,n-1),1) + diag(ones(1,n-1),-1); toc
Elapsed time is 1.06898 seconds.

Checking a recent version of Matlab:

Matlab 2024b:

-----------------------------------------------------------------------------------------------------
MATLAB Version: 24.2.0.2712019 (R2024b)
Operating System: Microsoft Windows 11 Enterprise Version 10.0 (Build 22631)
-----------------------------------------------------------------------------------------------------
>> n = 10000; m = n;
>> tic;B = diag(-2*ones(m,1),0) + diag(ones(m-1,1),-1) + diag(ones(m-1,1),1); toc
Elapsed time is 0.530463 seconds.
>> tic;A = diag(-2*ones(1,n)) + diag(ones(1,n-1),1) + diag(ones(1,n-1),-1); toc
Elapsed time is 0.527413 seconds.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.