2

I've converted a MATLAB code to C++ to speed it up, using the Armadillo library to handle matrix operations in C++, but surprisingly it is 10 times slower than the MATLAB code!

So I test the Armadillo library to see if it's the cause. The below code is a simple test code that initializes two matrices, adds them together and saves the result to a new matrix. One section of code uses the Armadillo library and other one doesn't. The section using Armadillo is too slow (notice the elapsed times).

Does it really slow down the execution (though it is supposed to speed it up) or am I missing some thing?

#include<iostream>
#include<math.h>
#include<chrono>
#include<armadillo>
using namespace std;
using namespace arma;
int main()
 {
   auto start = std::chrono::high_resolution_clock::now();
   double a[100][100];
   double b[100][100];
   double c[100][100];
   for (int i = 0; i < 100; i++)
     {
       for (int j = 0; j < 100; j++)
         {
           a[i][j] = 1;
           b[i][j] = 1;
           c[i][j] = a[i][j] + b[i][j];
         }
     }

     auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = finish - start;
    std::cout << "Elapsed time: " << elapsed.count() << " s\n";
    auto start1 = std::chrono::high_resolution_clock::now();
    mat a1=ones(100,100);
   mat b1=ones(100,100);
   mat c1(100,100);
   c1 = a1 + b1;
   auto finish1 = std::chrono::high_resolution_clock::now();
   std::chrono::duration<double> elapsed1 = finish1 - start1;
   std::cout << "Elapsed time: " << elapsed1.count() << " s\n";
   return 0;


}

Here is the answer I get:

Elapsed time: 5.1729e-05 s
Elapsed time: 0.00025536 s

As you see, Armadillo is significantly slower! Is it better not to use the Armadillo library?

MAh2014
  • 147
  • 10
  • 1
    Possible duplicate of [Performance Tradeoff - When is MATLAB better/slower than C/C++](https://stackoverflow.com/questions/20513071/performance-tradeoff-when-is-matlab-better-slower-than-c-c) – Wolfie May 17 '18 at 08:22
  • @Wolfie Thanks for the provided link,I've checked that,but I'm asking if armadillo library may slows down the code,as in the code I've attached. – MAh2014 May 17 '18 at 08:31
  • Why are you using a library to help with matrix operations, then manually completing the matrix operations? Coming from a MATLAB perspective I don't know the [C++/Armadillo syntax](http://arma.sourceforge.net/docs.html#operators), but surely you want to eliminate the loops when using Armadillo and do `mat c1 = a1 + b1`? – Wolfie May 17 '18 at 08:42
  • Your code has no MATLAB whatsoever, what its the relationship with MATLAB with the question at hand? – Ander Biguri May 17 '18 at 08:42
  • @AnderBiguri As Armadillo is for developing MATLAB like code in C++ ,I thought maybe people expert in Matlab might have used it and knows if it really speeds it up. – MAh2014 May 17 '18 at 08:49
  • 1
    @MAh2014 Its not for developing MATLAB code in C++, its to do maths in C++, with no relationship to MATLAB. Also, MATLAB is the fastest code there is for matrix operations, as it uses very highly optimized libraries under the hood. If you think you can write faster linear algebra code than what MATLAB can do, either you are wrong, or you can get very rich. I bet its the first ;) – Ander Biguri May 17 '18 at 08:51
  • @Wolfie Yes you're right,I know that,but I wanted it to perform just like the code section not using armadillo to see if calling this library consumes time.Of course I'm beginner in this issue,I will be glad to know if I'm missing something. – MAh2014 May 17 '18 at 08:54

2 Answers2

4

First of all make sure that the blas and lapack library are enabled, there are instructions at Armadillo doc. The second thing is that it might be a more extensive memory allocation in Armadillo. If you restructure your code to do the memory initialisation first as

#include<iostream>
#include<math.h>
#include<chrono>
#include<armadillo>
using namespace std;
using namespace arma;
int main()
 {
   double a[100][100];
   double b[100][100];
   double c[100][100];
   mat a1=ones(100,100);
   mat b1=ones(100,100);
   mat c1(100,100);

   auto start = std::chrono::high_resolution_clock::now();
   for (int i = 0; i < 100; i++)
     {
       for (int j = 0; j < 100; j++)
         {
           a[i][j] = 1;
           b[i][j] = 1;
           c[i][j] = a[i][j] + b[i][j];
         }
     }
   auto finish = std::chrono::high_resolution_clock::now();
   std::chrono::duration<double> elapsed = finish - start;
   std::cout << "Elapsed time: " << elapsed.count() << " s\n";

   auto start1 = std::chrono::high_resolution_clock::now();
   c1 = a1 + b1;
   auto finish1 = std::chrono::high_resolution_clock::now();
   std::chrono::duration<double> elapsed1 = finish1 - start1;
   std::cout << "Elapsed time: " << elapsed1.count() << " s\n";

   return 0;
}

With this I got the result:

Elapsed time: 0.000647521 s
Elapsed time: 0.000353198 s

I compiled it with (in Ubuntu 17.10): g++ prog.cpp -larmadillo

Claes Rolen
  • 1,446
  • 1
  • 9
  • 21
  • Are you suggesting that using armadillo will speed up matrices operations?and do you think that if I convert matlab matrices operations to exactly the same code in C++ using armadillo (matrices operations are mostly nothing more than multiplication) it will speed up? – MAh2014 May 17 '18 at 19:52
  • Armadillo will not likely be faster than Matlab if you are using matrix/vector operations, however if you are iterating a calculation in loops in Matlab you will gain performance using Armadillo/C++. An interesting comparison would be to do the two variants (loop and matrix) in Matlab, the difference would most probably be larger in Matlab than the Armadillo code – Claes Rolen May 17 '18 at 20:07
  • but It is implemented in a low level language(C++),doesn't it result in faster execution even though matlab is doing better in matrice operations? – MAh2014 May 17 '18 at 20:32
2

I believe the problem comes from you not using armadillo at all. You uniquely used it to create variables that are a bit more complex than normal 2D arrays of C++, but really nothing more. What Armadillo can do for you is give you very fast matrix operations, as in c1=a1+b1;, without loops.

But if you just instead write it as an elemetwise operation, you are just not using armadillo. Its the same as using MATLAB for matrix multiplication but writing the matrix multiplication yourself. You are not using MATLAB's libraries then!

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • So If I remove the loop and simply add the matrices, It has to be faster than not using armadillo,but it is not.after all does converting matlab code to C++ using armadillo will speed it up ?I replaced the code without loop. – MAh2014 May 17 '18 at 09:12
  • @MAh2014 no, converting code from MATLAB to armadillo will not speed it up in general, depends on your problem. Sometimes, depends on the task at hand, converting MATLAB to a low level language can speed it up. But Armadillo specifically is for linear algebra operation, something that the software MATrix LABoratory does exceptionally well. There is however no real answer to "will it be faster" as it highly depends on your problem. – Ander Biguri May 17 '18 at 09:16