Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why MATLAB is faster than C++ in creating random numbers?

I have been using MATLAB for a while for my projects and I have almost never had an experience in C++.

I needed speed and I heard that C++ can be more efficient and faster than MATLAB. So I tried this:

I created a matrix of random numbers using rand(5000,5000) on MATLAB.

And in C++, I have initialized a 2D vector created 2 for loops each of them looping for 5000 times and each time. MATLAB was 4-5x faster, so I thought it is because matlab executes vectorized codes in parallel, then I written the C++ code using parallel_for. Here is the code:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <ppl.h>
using namespace std;
using namespace concurrency;
int main();
{
    int a = 5000, b = 5000, j, k;
    vector< vector<int> > vec(a, vector<imt>(b));
    parallel_for(int(0), a, [&](int i) {
        for (j = 0; j <b; j++)
        {
            vec[i][j] = rand();
        }
    });
}

So the code above is about 25% faster than MATLAB's rand(5000,5000) Yet C++ is using 100% of the CPU while MATLAB is using 30% of CPU.

So I forced MATLAB to use all of the CPU by running 3 instances of MATLAB using rand(5000,5000) and divided the time it takes by 3. It made MATLAB twice as fast as C++.

I wonder what am I missing? I know this is a tiny example but I need an answer to be sure to port my code to C++.

Current status:

When I write C++ code without parallel_for I get half of the MATLAB's speed with the same CPU usage. Yet people who gave answers say that they are almost the same. I do not understand what I am missing

here is a snapshot of the optimization menu enter image description here

like image 280
Serdar Oztetik Avatar asked Jan 21 '26 09:01

Serdar Oztetik


1 Answers

When you call rand(5000,5000) in Matlab, Matlab executes the command by calling Intel MKL library, which is a highly optimized library written in C/C++ with lots of hand-coded assembly.

MKL should be faster than any straightforward C++ implementation, but there is an overhead for Matlab to call external library. The net result is that, for random number generation in smaller sizes (less than 1K for instance), plain C/C++ implementation will be faster, but for larger sizes, Matlab will benefit from super optimized MKL.

like image 127
PhD AP EcE Avatar answered Jan 24 '26 05:01

PhD AP EcE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!