M
M
mlyamasov2021-07-16 14:42:47
Programming
mlyamasov, 2021-07-16 14:42:47

What is the best way to allocate an array of complex numbers in RAM in terms of performance?

In two arrays: double re[N]; doubleim[N]; or in one double c[2*N], where the real and imaginary parts alternate?
Is there any difference in terms of performance? Maybe in the second case, the processor cache works better, because. Are the real and imaginary parts of a number used at the same time?
It is more convenient for me in two arrays.
PS I know about structures and readability.
Also, I remember that some popular math libraries keep the real and imaginary parts separate. For example, UMFPACK supports both options. Logic also tells me that it is better to use one array, but I'm not sure, I would like some confirmation, test results ...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Wataru, 2021-07-16
@wataru

If you want to be cache friendly, then you need data access locality.
Therefore, it is better to alternate real and imaginary parts in one array. Or, even better, create a structure with two fields and store an array of them. Here in memory, the arrangement of data will be the same, but the code will be readable and logical.

R
res2001, 2021-07-16
@res2001

It is more convenient to use an array of structures:

struct complex_t {
float re, im;
};

The meaning is the same as in c[2*N], but it is more convenient to use and the readability of the code increases.
Note that operations on doubles usually take longer than operations on floats.

G
Griboks, 2021-07-16
@Griboks

Is there any difference in terms of performance?

It depends on the processing algorithm. If you just load 2n random numbers into memory, then it will take the same amount of time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question