C vs C ++ for numerical modeling (performance)

I'm going to write a limited diffusion distribution (DLA) simulation, and I'm wondering if to use C or C ++.

C ++ would be good for design reasons, but I wonder if C will work better. Of course, I know about the effectiveness of the algorithm and have chosen the best possible algorithm. Therefore, I am not talking about improving O (n ^ 2) to O (log n) or the like. I am trying to reduce my constants, so to speak.

If you don’t know DLA, it basically comes down to having an array of doubles (size between 10 ^ 3 and 10 ^ 6) and in a loop in which random twins are selected for comparison (larger / smaller) with larger parts of the array.

Thus, the difference in performance that matters is the data access and call functions:

  • Data Access: C struct vs. C ++ class with public data elements versus C ++ class with private data members and accessories.
  • Call functions: C functions versus C ++ member functions.
I mean, what is the final way to judge this - is to look at the assembly code (for example, comparing the number of moves / loads, transitions and calls)? This, of course, depends on the compiler (for example, you can compare the awful C compiler with a good C ++ compiler). I use Gnu compilers (gcc and g ++).

I found that the assembly created by gcc and g ++ is almost identical in terms of the number of transitions (none), movements / loads, and calls for the following two programs:

Program C

#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 a C ++ program, of course I get another call in the assembly when I call the particles [0] .setx (60.42).

Does this mean that I could also choose C ++ as C, since they produce almost the same build code? Should I avoid member private data as it adds extra function calls (for example, does the assembly cost expensive)?

+6
source share
2 answers

Given the types of things you state, I would be surprised to see a significant advantage for C for any of them. Based on what you said, I also assume that your comparisons are based on compilation with or without minimal optimization. If full optimization is enabled, I expect even those to disappear.

In the long run, C ++ offers more options for optimization. One that is fairly common with matrix arithmetic (although I'm not sure if this applies to your DLA modeling) is expression patterns that you can use to β€œsmooth” the calculation to avoid copying data that would otherwise be needed.

Bottom line: in the worst case, C ++ will be exactly equivalent to C (i.e., in the worst case, you should write your C ++ code in much the same way as C code, and you don’t see a difference in performance). In the best case, additional C ++ features (especially templates) offer you the opportunity to optimize in ways that are either impossible or extremely impractical with C.

+8
source

In the event that you send a message, you basically set the performance difference between malloc and new .

In C ++, the new operator performs additional functions, such as calling the constructor of an object or initializing variables.

For an equivalent operation in C, you will need to initialize the variables after calling malloc . Change your program to C to behave like C ++ and then profile.

In most performance issues, you need to compare apples to apples. If you use object-oriented programming in C ++, you will need to code the equivalent in C ++ before performing comparisons. Given this, in most cases there is no marginal performance difference between the two languages.

Also consider: development time, correctness, reliability, security, and your confidence or experience with the language. Performance is usually negligible compared to other project attributes.

Many performance issues do not depend on language, but on design and platform. Cache omissions, loop reversal, many comparisons, can affect performance regardless of language.

+1
source

Source: https://habr.com/ru/post/945841/


All Articles