Overhead of using classes for a matrix of algebraic structures in C ++

I use C ++ to code the complex FFT algorithm, so I need to implement algebraic structures such as quaternions and Hamilton-Eisenstein codes. The algorithm works with a 2D array of these structures. What would be the overhead of implementing them as classes? Otherwise, should I create an array with the parameters [M] [N], which consists of Quaternion classes, or should I create an array [M] [N] [4] and work with [4] arrays as quaternions? Using classes is more convenient, but creating M * N classes and accessing their methods instead of working with just an array - isn't that too much overhead? I am coding an algorithm for processing large images, so performance is important to me.

+6
c ++ class algebraic-data-types overhead quaternions
source share
2 answers

IMHO you better serve by implementing them as classes, simply because it will allow you to write your code faster with less errors. You have to take measurements to find out what works best if it matters to you, but also make sure that this code is actually a performance bottleneck. (Mandatory Quote by Donald Knuth : “premature optimization is the root of all evil”).

Most compilers will work very well to optimize the code for you, I would say. More often than not, I find it to be something other than these low-level things that matter, like adding an early test or minimizing a dataset or something else.

For the quaternion, you can still implement the class using an array inside (in case it happens faster), which should make the difference even less important.

You are probably best served, for example, to make sure you can run your algorithms in parallel on multi-core machines or do your actual calculations using SSE instructions.

+2
source share

As for the service data of classes: if your classes do not have virtual functions, there is no penalty for using classes.

So, for example, an array of complex variables can be written as:

std::complex<double> m[10][10]; 

Beware of the classes in the STL collection, however, as they tend to use dynamic allocation and sometimes create significant overhead (i.e. I would not make arrays using vector< vector<> > .

You might want to learn about using a library like Eigen for fast, optimized, matrix / vector classes.

+2
source share

All Articles