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