9

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.

6
  • 1
    Are those arrays static data? Commented Apr 5, 2012 at 21:28
  • They are global declarations. Commented Apr 5, 2012 at 21:31
  • 1
    Who knows what optimizations g++ tries to do. Perhaps it allocates such an array to check to warn about invalid memory access. Try to run the benchmark again (perhaps do it multiple times) with -O0 and do the compilation and linking separately. Commented Apr 5, 2012 at 21:35
  • Could be related to this bug or one mentioned in there: gcc.gnu.org/bugzilla/show_bug.cgi?id=20923 What's the gcc version you are using? Commented Apr 5, 2012 at 21:39
  • 3
    Globals/statics are required by the standards (both C and C++) to be intialized to 0. Now, GCC typically puts such data in the .bss section. You can do the following: 1) Check the .bss sections for both values and 2) Look at the assembler outputs. Try passing in -fno-zero-initialized-in-bss option to the compiler and see if it makes any difference. Commented Apr 5, 2012 at 21:39

2 Answers 2

10

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:

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.