mutelogan
mutelogan

Reputation: 7087

Does g++ compilation time depend on array size?

I have a C++ code which has 3 array declarations.

float A[NUM]; float B[NUM]; float C[NUM];

When I compile with NUM=512, compilation is fast

time g++ -DNUM=512 trials trials.cpp -lm

0.16s user 0.04s system 94% cpu 0.219 total

However, when I compile with NUM=167772160, it takes more time.

time g++ -DNUM=167772160 trials trials.cpp -lm

7.90s user 0.69s system 99% cpu 8.604 total

I haven't used C++ in years. I am curious to know why there is a time difference in compilation though the object files after the compilation are of the same size.

Upvotes: 9

Views: 342

Answers (2)

Ling Kun
Ling Kun

Reputation: 11

Is your array declared locally or globally? If it is globally, because the linker should allocate memory in .data section, this may take a long time. However, if you declare it locally, because the memory is allocated at run-time, not link time. It will be linker's problem, but a problem caused by the analyzer or optimizer of compiler.

Upvotes: 0

sehe
sehe

Reputation: 392989

This is quite a wellknown conundrum. Somewhere along the way, the actual memory for the array is going to be allocated

See: Linker performance related to swap space?

It would appear that, as we might have suspected, it looks like ld is actually trying to anonymously mmap the entire static memory space of this array (or possibly the entire program, it's hard to tell since the rest of the program is so small, it might all fit in that extra 4096).

Also related:

Upvotes: 10

Related Questions