I am trying to provide some answers to a long disputed question on: "which programming language is the fastest when handling large matrices and arrays. Fortran or C++". Personally I use Fortran but after reading some very interesting suggestions and experiences by users that can and did program with both fortran and C++ languages (without providing any evidence... just discussing... but interesting stuff nonetheless) I thought of trying C++ myself and performing a small experiment in an attempt to find some answers.

I developed a very simple algorithm which is given below in C++. This code performs some calculations with a square 10000x10000 matrix named jimmy looping the procedure 100 times, while a second part of the algorithm handles an array named jimmy2 (100 million elements) and loops also 100 times the performed calculations. By the way, I must note here that the code for the loops in C++ was found in the net due to my poor capabilities in C++ (I had to start from somewhere).

Given that I do not have that much of experience with C++ compilers, I would like someone who does have this experience, to take this algorithm and first of all optimize it, if he/she thinks that I programmed any of its parts in a non optimal manner (most probably I did), and then compile it in a 32-bit machine by using the latest C++ compiler and post the exe file in this conversation. To maintain a level of clarity, please repost the code in C++ if any reprogramming was done.

Now, what I did, was to compile the C++ code with the default compiler incorporated in VS2013 and by using Full Optimization (/Ox) in the release configuration, I got the following average times in ms (I run an Intel(R) Core(TM) i5 CPU M520 @2.4GHz on a personal laptop):

ms1: 11900 (matrix 10000x10000)

ms2: 11400 (array 100,000,000)

Those that might be interested in participating here must know that the corresponding average times, when using Visual Intel Fortran Compiler XE14 , were:

ms1: 5300 (matrix 10000x10000)

ms2: 4900 (array 100,000,000)

The algorithm developed in F90 is also given and can be found at the end of this post.

Waiting to hear from you C++ experts, with anticipation.

P.S. If any C or C# or Python or other language users would like to give it a go the same things apply. Post your code and the .exe file so as to derive the corresponding times. No GPU allowed given that this has as a general goal to compare CPU compilers.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

C++ Code

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include

#include

#define WIDTH 10000

#define HEIGHT 10000

int jimmy[HEIGHT][WIDTH];

int jimmy2[HEIGHT * WIDTH];

int n, m, ii, DumDum1, DumDum2;

using namespace std;

int MS2(int Dum)

{

int Dum2;

// Check for array use

auto begin2 = chrono::high_resolution_clock::now();

for (ii = 0; ii < 100; ii++)

for (n = 0; n < HEIGHT; n++)

for (m = 0; m < WIDTH; m++)

{

jimmy2[n*WIDTH + m] = (n + 1)*(m + 1);

}

auto end2 = chrono::high_resolution_clock::now();

auto dur2 = end2 - begin2;

auto ms2 = std::chrono::duration_cast(dur2).count();

Dum2 = ms2;

return Dum2;

}

int MS1(int Dum)

{

int Dum1;

//Check for matrix use

auto begin = chrono::high_resolution_clock::now();

for (ii = 0; ii < 100; ii++)

for (n = 0; n < HEIGHT; n++)

for (m = 0; m < WIDTH; m++)

{

jimmy[n][m] = (n + 1)*(m + 1);

}

auto end = chrono::high_resolution_clock::now();

auto dur = end - begin;

auto ms = std::chrono::duration_cast(dur).count();

Dum1 = ms;

return Dum1;

}

int main() {

DumDum1 = MS1 (1);

DumDum2 = MS2 (2);

cout

Similar questions and discussions