Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C vs. C++ for numeric simulation (performance)

I am about to write an off-lattice diffusion limited aggregation (DLA) simulation and I am wondering whether to use C or C++.

C++ would be nice for design reasons but I am wondering if C would perform better. Of course I know about algorithm performance and have chosen the best possible algorithm. So I'm not talking about improving O(n^2) to O(log n) or similar. I'm trying to reduce my constants so to speak.

If you do not know DLA it basically boils down to having an array of doubles (size between 10^3 and 10^6) and in a loop choosing random doubles to compare (greater/less than ) to large portions of the array.

So performance differences which would matter for this is data access and calling functions:

  • Data access: C struct vs. C++ class with public data members vs. C++ class with private data members and accessors.
  • Calling functions: C functions vs. C++ member functions.

Am I right to conclude that the ultimate way to judge this is to look at the assembly code (e.g. comparing the number of moves/loads, jumps and calls)? This is of course compiler dependent (e.g. you could compare an awful C compiler to a good C++ compiler). I'm using the Gnu compilers (gcc and g++).

I've found that the assembly produces by gcc and g++ is almost identical in terms of the number of jumps (none), moves/loads and calls for the two following programs:

C program

#include <stdlib.h>

typedef struct 
{
    double x;
} particle;

double square(double a)
{
    return a*a;
}

int main()
{

    particle* particles = malloc(10*sizeof(particle));
    double res;

    particles[0].x = 60.42;

    res = square(particles[0].x);

    return 0;
}

C++ program

class particle
{
    public:
        double x;

    public:
        double square()
        {
            return x*x;
        }

};

int main()
{

    particle* particles = new particle[10];
    double res;

    particles[0].x = 60.42;

    res = particles[0].square();

    return 0;
}

If I use private member data in the C++ program I of course get another call in the assembly when I call particles[0].setx(60.42).

Does this mean I might as well choose C++ as C since they produce almost the same assembly code? Should I avoid private member data since it adds extra function calls (e.g. is call in assembly expensive)?

like image 740
Wuhtzu Avatar asked May 25 '13 16:05

Wuhtzu


1 Answers

Given the types of things you're outlining, I'd be surprised to see a significant advantage to C for any of it. Based on what you've said, I'd also guess the comparison(s) you've done are based on compiling with little or no optimization enabled. With full optimization enabled, I'd expect even those to disappear.

In the long term, C++ offers more chances for optimization. One that's fairly common with matrix arithmetic (though I'm not sure it's applicable to your DLA simulation) is expression templates, which you can use to "flatten" a computation to avoid copying of the data that would otherwise be necessary.

Bottom line: at very worse, C++ will end up precisely equivalent to C (i.e., in the very worst case, you'd write your C++ code virtually the same as C code, and see no difference in performance). At best, the extra features of C++ (especially templates) offer you chances to optimize in ways that are either impossible or grossly impractical with C.

like image 198
Jerry Coffin Avatar answered Sep 30 '22 18:09

Jerry Coffin