Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does g++ compilation time depend on array size?

Tags:

c++

g++

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.

like image 368
mutelogan Avatar asked Apr 05 '12 21:04

mutelogan


1 Answers

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:

  • Long compilation time for program with static allocation
  • Strange GCC Behaviour
  • missing optimization in gc: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=4131
like image 88
sehe Avatar answered Oct 30 '22 12:10

sehe