Vector addition benchmark in C, C++ and Fortran
Posted on April 11, 2012 by Sol

If you were involved in working with large arrays and linear algebra, you’ve probably heard mantras like A Fortran implementation can usually achieve more performance than C or C++ or C++ is slow for scientific computations, don’t use it!. Until recently I honestly believed that, at least for elementary linear algebra operations, like vector addition for e.g., C can easily beat C++ (specifically that working with C-arrays should be faster than the more elaborate vector data structure from C++) and Fortran will beat both. Please be aware that I’m not talking about which language is inherently superior to another, but rather about what is the actual state of their implementation on personal computers running Linux and Windows.

In order to test the above myths I’ve implemented a few simple codes for vector, one dimensional arrays, addition in C, C++ and Fortran.

Adding two vectors is a simple operation you just need to pass through data only once and add element by element. A straightforward C implementation that adds 90 millions doubles is presented next:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static size_t MM = 90000000;

void add(double *a, double *b, double *c, size_t n){
    size_t i;
    for(i = 0; i < n; ++i) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    double *a, *b, *c;
    size_t i;
    clock_t start,end;
    double diffs;

    a = (double *) malloc(MM*sizeof(double));
    b = (double *) malloc(MM*sizeof(double));
    c = (double *) malloc(MM*sizeof(double));


    for(i = 0; i < MM; ++i){
        a[i] = 1.0/(double)(i+1);
        b[i] = a[i];
    }

    start = clock();
    add(a,b,c,MM);
    end = clock();
    diffs = (end - start)/(double)CLOCKS_PER_SEC;

    printf("time = %lf\n",diffs);

    free(a);
    free(b);
    free(c);
    return 0;
}

The above code will simply report how much time it takes to add two 90 millions elements vectors on a given machine. If you have a C99 compatible compiler you can also use the restrict keyword:

1
2
3
void add(double *restrict a, double *restrict b, double *restrict c, size_t n){
...
}

On a modern core i7 processor with 16 GB or RAM the above code takes about 0.6 seconds with no optimizations and about 0.35 seconds with maximum compiler optimization. There was practically no speed difference between the original code and the one with the restrict keyword.

Let’s see the equivalent C++ implementation with STL vectors instead of C-arrays:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <ctime>

using namespace std;

static size_t MM = 90000000;

void add(const vector<double> &a, const vector<double> &b, vector<double> &c){
    size_t n = a.size();

    for(size_t i = 0; i < n; ++i) {
        c[i] = a[i] + b[i];
    }
}

int main() {
    vector<double> a(MM), b(MM), c(MM);

    for(size_t i = 0; i < MM; ++i){
        a[i] = 1.0/(double)(i+1);
        b[i] = a[i];
    }

    clock_t start = clock();
    add(a,b,c);
    clock_t end = clock();
    double diffs = (end - start)/(double)CLOCKS_PER_SEC;

    cout << "time = " << diffs << endl;

    return 0;
}

The code is shorter and, at least to me, it looks cleaner. What about the performance ? Well … this code takes about 1.23 seconds with zero optimizations! More than double the time of the C version. However if you enable full compiler optimizations this will take 0.23 seconds!

Both tests were done on the same machine and same operating system (Windows 7 64 bits) with the same compiler (Visual Studio 2010), each test was done ten times and the results were averaged. Maybe Microsoft has a crappy C compiler, after all they have no support for C99 …

Repeating the above two tests with gcc-4.7 and g++-4.7 on a Linux machine (64 bits Linux Mint) I’ve obtained 0.755 seconds and 0.345 seconds (with optimizations) for the C version; versus 1.1 seconds and 0.22 seconds (with optimizations) for C++. Can you see a pattern here ?

Apparently the compiler, for both GCC and Visul Studio, does a better optimization job for C++ than for C.

In true C++11 spirit we can further modify the add function:

1
2
3
4
void add(const vector<double> &a, const vector<double> &b, vector<double> &c){
    transform(a.begin(),a.end(),b.begin(),c.begin(), 
       [] (double a, double b) { return a + b; });
}

This time the code is slightly slower on Windows with Visual Studio 1.29 seconds and 0.25 seconds (with optimizations) versus Linux and gcc where the code takes about 2.23 seconds and 0.22 seconds (with optimizations).

What about Fortran ? First, I’ve used a trial version of Intel Fortran for both Linux and Windows, from past experiences I knew Intel Fortran is one of the fastest Fortran compilers available for Intel processors.

Next is a straightforward Fortran implementation of vector addition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
subroutine add(a,b,c,n)
    integer :: n,i
    double precision :: a(n),b(n),c(n)

    do i = 1,n
        c(i) = a(i) + b(i)
    enddo
end subroutine add

program test
    integer,parameter :: MM = 90000000
    double precision, allocatable :: a(:),b(:),c(:)
    double precision :: start,finish

    allocate(a(MM),b(MM),c(MM))
    
    do i = 1,MM
        a(i) = 1.0/dble(i)
        b(i) = a(i)
    enddo
    
    call cpu_time(start)
    call add(a,b,c,MM)
    call cpu_time(finish)

    print*,'time =',(finish-start)

    deallocate(a,b,c)
end program test

If you are a modern Fortran priest you may notice that I’ve used an explicit loop for vector addition, I did this because, initially, I wanted to give a fair chance to C and C++. The above code scores, on Windows, about 0.66 seconds without compiler optimization which is way better than both C and C++ without optimizations. However, the optimized version takes about 0.29 seconds which is slightly worse than C++ but a much better score than C. The results on Linux are slightly worse.

In most modern Fortran books and tutorials you are advised to avoid the use of explicit loops for vector and array operations, the code is cleaner and the compiler can potentially use more optimizations for array operations. Having said that, we can replace the add subroutine with a single instruction:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
program test
    integer,parameter :: MM = 90000000
    double precision, allocatable :: a(:),b(:),c(:)
    double precision :: start,finish

    allocate(a(MM),b(MM),c(MM))
    
    do i = 1,MM
        a(i) = 1.0/dble(i)
        b(i) = a(i)
    enddo
    
    call cpu_time(start)
    c = a + b
    call cpu_time(finish)

    print*,'time =',(finish-start)

    deallocate(a,b,c)
end program test

The above code is actually a bit slower slower than the one with the explicit do loop, it takes about 0.89 seconds and 0.29 seconds (with optimizations).

The six codes used for this benchmark are available on GitHub together with an Excel file that contains the recorded result for each run.

If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

If you need to brush your Fortran knowledge a good book is Modern Fortran Explained by M. Metcalf, J. Reid and M. Cohen:

blog comments powered by Disqus