I have a Matlab code computing the solution to an eigenvalue PDE. It consists of two parts: assembly of the stiffness/rigidity matrices and solving a generalized eigenvalue problem. I mention that the assembly is done using vectorized code in Matlab. It is likely that I would need to run the program on a quite large database of test-cases (a few million cases). In this case, performance in terms of computational time becomes noticeable.

I was wondering what could be the gain if I tried to code the algorithm in C/C++ ? (not sure I am capable of that... :) but I want to weigh the potential benefits)

I guess assembly could go faster, but the eigenvalue solver is pretty efficient in Matlab.

What is your experience with situations like this? Can coding in a faster language give important computation time reduction compared to Matlab?

More details on the problem as requested:

  • eigenvalue computation for the Laplace-Beltrami operator on a portion of the sphere
  • I use Lagrange P1 finite elements on a triangular mesh of the surface domain
  • example of Matlab computation time for roughly 780000 points (degrees of freedom): assembly 3 seconds, eigenvalue computation 26 seconds

Apparently, the significant part of the computation is finding the eigenvalue. Maybe using SlepC could help with this... (In some old manual I saw a SlepC interface for Matlab, but not in the new ones...)

  • 4
    $\begingroup$ Assuming that you've profiled your code and have found that assembly takes a significant amount of time, you might consider writing a MEX file in C/C++ and call it from Matlab. If you're proficient at C/C++, you will likely be able to realize a substantial speedup during assembly (I'm not going to guess at a magnitude without knowing your code). This allows you to keep the majority of your code in Matlab and just speed up the slow parts incrementally. $\endgroup$ Commented Jan 31, 2018 at 15:50
  • 2
    $\begingroup$ Have you seen this note by Davis on creating sparse matrices in matlab? blogs.mathworks.com/loren/2007/03/01/… $\endgroup$ Commented Jan 31, 2018 at 18:55
  • $\begingroup$ @BillGreene Thank you for the note. I was aware of the use of sparse matrices, and the assembly procedure uses this sparse formulation. $\endgroup$ Commented Jan 31, 2018 at 20:04
  • $\begingroup$ Can you provide some more details. What is the equation? Are you doing 2D or 3D? What elements are you using? Using my vectorized code I can assemble 3D/tetrahedron/P1 laplace operator with 2 million elements / 300 000 DOFs in 3,5 seconds. Even in this case the solution of the linear system will be the bottle neck. Your problem seems to be embarrassingly parallel. Do you have access to multiple computing nodes? Even some set of workstations would be fine. In my university we often use idle workstations at night to compute stuff like this. $\endgroup$
    – knl
    Commented Feb 1, 2018 at 8:35
  • $\begingroup$ @knl I am aware that that it is possible to immediately parallelize this, since there is no communication between problems. I was just interested to see if changing from Matlab to C could bring large improvements or not. $\endgroup$ Commented Feb 1, 2018 at 9:42

1 Answer 1


It is unlikely that you can benefit a lot from rewriting this particular code in C++.

Main reasons:

  • you are already assembling a sparse matrix using Matlab-specific framework for sparse matrices
  • the hotspot is the eigenvalue computation, for which Matlab will use a call to a highly-optimized LAPACK implementation. Which would be similar to what you would be able to achieve with C++.

Implementation in C++ would usually give you benefits:

  • better flexibility in using external libraries (not necessarily easily done)
  • better options for parallelization and GPU usage (again, efficient coding for parallel computations usually requires significant efforts)
  • options to grow your code in a maintainable and extendible fashion
  • be more efficient in functions that are not "wrappers over linear algebra calls"

Now, none of that, as far as I see, immediately applies to your specific case.

I see, from where your question on SLEPc usage from Matlab comes. I would say, that is your best bet here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.