Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::complex in struct that makes compile slow

Tags:

c++

I noticed that the following code is extremely slow to compile: (It can't even finish on my computer)

#include <complex>

struct some_big_struct {
    std::complex <double> a[1000000][2];
};

some_big_struct a;

int main () {
    return 0;
}

Out of curiosity, I've also tried other alternatives of the code. However, these codes seem to compile just fine on my computer :

#include <complex>

struct some_big_struct {
    double a[1000000][2];
};

some_big_struct a;

int main () {
    return 0;
}

and

#include <complex>

std::complex <double> a[1000000][2];

int main () {
    return 0;
}

I wonder if anyone can share some insight on why such is the case. Thanks!

like image 854
Nisiyama Suzune Avatar asked Jan 19 '26 09:01

Nisiyama Suzune


1 Answers

The compiler is probably running the default std::complex constructor while compiling, so that it can put the initialized values of all the array members into the executable, rather than generate code that performs this loop when the program starts. So it's calling the constructor 2 million times while compiling.

like image 187
Barmar Avatar answered Jan 21 '26 01:01

Barmar