-3

I often see programmers, especially here on StackOverflow, who indicate the compilation time of their respective programs and with that sometimes intend to reduce these corresponding periods, even if the result would be only just subtle.

While, of course, i can understand that, when a programmer is in the development process of any program and the back and forth rhythm of creating, testing and editing the source code and with that to keep the time intervals between these processes short is important, but i just can not understand why is this such a big matter if the compilation time is only a few milliseconds?

And even if it would be a few seconds, why then either? We just could do a little bit of relaxing from the coding or anything else in that time.

Are we just too impatient?

Or is there something else i am missing about a disguised indication of somewhat by the compile-time?

Why is the Duration of the compile-time essential important? Does it indicate somewhat?

  • 2
    If it is only a few milliseconds, then it is likely not important. Check the instances where you hear these things and check if they're referring to total compile times of less than 100 milliseconds. – L. Scott Johnson Dec 19 '19 at 14:17
  • 5
    Programmers are not machines that write the perfect and correct solution on their first try. We have a workflow of writing a program, compiling it and testing, and then re-writing because the first (and second and..) attempt was wrong. If we have to wait for the compilation part, that slows down the entire workflow. And time is money. If one manages to optimise the compilation time down to a few milliseconds, then we can move on to testing our changes. If we need to wait hours until that step, that creates a bottleneck in the development. P.S. Try compiling libreoffice or clang on your laptop. – eerorika Dec 19 '19 at 14:26
  • 11
    *"if the compilation time is only a few milliseconds"* - Have you tried building real live applications? It may take several hours. Compilation of C++ is extremely inefficient. – user7860670 Dec 19 '19 at 14:29
  • 1
    You are confused with run-time performance. – Paul Ogilvie Dec 19 '19 at 14:47
  • 3
    It may take less than a second to compile an individual source file, but building an entire system can take considerably longer. I've worked on systems that literally took *hours* to build. – John Bode Dec 19 '19 at 15:26
  • 2
    ***Why is the Duration of the Compile-time important?*** It becomes important when it interrupts your ability to work. I mean when you press compile and have to wait 5+ minutes before you can do any testing you will eventually try to optimize the process. I am talking here about professional programming not code that you build only a few times. – drescherjm Dec 19 '19 at 15:50

2 Answers2

5

i just can not understand why is this such a big matter if the compilation time is only a few milliseconds?

Because it isn't a matter of "a few milliseconds". Only tiny and/or toy applications take such a short time to compile.

When people complain about C++ compile times, they're complaining about a timescale of several minutes or hours (day-long full rebuilds are not unheard-of for large projects). Large-scale C++ development has compile times that large. Touching the wrong header in a program can sometimes invoke a multi-hour compilation.

A programmer's job is to program. That means writing code, but also executing and testing it. Compile time is dead time during which none of that is really happening.

We just could do a little bit of relaxing from the coding or anything else in that time.

There is a psychological concept of "flow", where your mental headspace is tightly focused on the task you're doing. From a computer perspective, you could think of flow as being in a state where your instruction and data caches have all of the code and information needed to do a complex computation. So everything is moving as efficiently as possible.

"Relaxing" from flow is like dumping your caches. Even if it's just a short break. To get back into doing what you were doing, you now have to reload all that stuff back into memory. And yes, that doesn't take a huge amount of time, but it's far less efficient than if you didn't have that cache dump.

A gap of longer than a minute or so is like not just dumping your caches, but also your entire virtual memory. So now everything you try to do is a page-fault, requiring hard disk access. Again, you can get the job done, but it happens far slower than necessary.

So even if a programmer could make progress towards some other task, they're not working as efficiently as they could be.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • To complete your "system" description you may add that keeping the flow page around 80% is good rate, just to avoid "overheating". The other 20% is where compiling time "relax" makes sense. – Ripi2 Dec 19 '19 at 17:42
3

C programs are compiled quickly, or can be compiled quickly, at the expense of runtime performance of the generated executable.

Coding a C compiler which compiles quickly to slow running x86 executable is an easy exercise. Fabrice Bellard made TinyCC (less than 20k lines of C code). But in practice, you expect your C compiler (such as GCC) to be able to compile quite cleverly. This draft report of mine on Bismon give examples of clever optimizations. And that is why GCC is a ten millions lines of code monster.

The key concept is compiler optimization (loop unrolling, inline expansion, automatic vectorization, register allocation). It is an art, and as a problem it is unsolvable (because of Rice's theorem). Read the Dragon book for an introduction to compiler issues.

Famous mathematical problems can be reformulated as compiler optimization problems. Look on what Julia Robinson worked.

C++14 is slightly different to compile. Its standard C++ library define containers, which are difficult to compile, because template expansion in C++ is Turing complete. So some C++ weird programs are short but can take an unreasonable amount of time to be compiled.

Observe also that standard C++ headers are quite big: a simple #include <vector> expands to nearly ten thousands lines on my Linux GCC 9.

Future (C++20 perhaps) versions of C++ might have modules.

And you could enable link time optimizations (with GCC, compile and link with gcc -O2 -flto), which basically compile your code twice, grossly speaking.

Some large C or C++ programs can take hours of CPU to build. Google proprietary indexing code is rumored to be a single ELF executable compiled from more than 800 millions of C++ lines. Oracle database products are rumored to be half a billions lines of C++.

Some large systems have a lot of lines: for example, all the source code in a typical Linux distribution is about twenty billions lines of code (half of them being C or C++).

I am 60 years old, and old enough to remember when the C code I wrote alone took an hour of compile time.

With metaprogramming techniques, you can get a lot of "emitted" C code from a few thousand lines of input.

Cognitive science teaches us that a software developer lost focus (e.g. when thinking of a bug) in a few minutes, or even a few seconds.

Compiling the entire Linux kernel takes several minutes on a powerful desktop. Compiling the entire Qt toolkit (in C++), or the source of GCC 9, from the distributed source tarball takes several hours. Compiling Firefox can take more than a day.

if the compilation time is only a few milliseconds?

That never happened to me, except for hello world programs. And my desktop is a powerful AMD2970SX running Debian/Unstable. Even a small program like refpersys (6000 lines of C++ today december 19th 2019, git commit b1af17cb5e693efad0) take 3.2 seconds to build (using omake -j 10)

Please teach me how a small program like RefPerSys could be built in milliseconds.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547