Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out where the compiler spends its time?

How do I find out where the compiler spends its time?

My build is too slow, I'm already using a RAMdisk for the VC++ temporary files and it doesn't make any difference. (I have an SSD, so I expected no difference.)

Most single C++ files in this project take approx. 2 seconds to compile which seems awful (as I also don't have any in-project parallelization because I'm using VS2005).

How can I optimize this? Don't optimize without profiling, but how do I profile the compiler?

Note: These were not really helpful:

  • Profiling the C++ compilation process
  • Displaying build times in Visual Studio? (It isn't completely without merit though ... if you do a single-file compilation, instead of the whole project, it will show the time taken for a single file: That way I see that quite some files here take more that 5 seconds, some even 10 sec when compiled stand-alone)

Let's add a good comment: Do you have lot's of templates? How large are your files on average? Do you checked on including only the minimum of necessary headers

No, I did not. I could (and pro'lly will) of course. I may be able to find something, but it's all speculation and trial and error!

"Everybody" tells you that you should only optimize after measuring/profiling, but when optimizing compilation times, we're back to trial&error?


Note: The proposed "hack" from the comments with __TIME__ does not work, at least not on Visual-C++, because (as the docs state): The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss. -- I guess one could at least get timings for single compilations units with this stamp, but it doesn't help to drill down into an compilation unit.

like image 775
Martin Ba Avatar asked Mar 14 '13 09:03

Martin Ba


People also ask

How do you calculate compilation time?

Compile time calculation can be done using template metaprogramming. This will output the time required by g++ file_name. cpp to compile.

How do I check compile time in Visual Studio?

Go to menu Tools → Options → Projects and Solutions → Build and Run → MSBuild project build output verbosity. Set to "Normal" or "Detailed", and the build time will appear in the output window.

Why does compiling code take so long?

Every single compilation unit requires hundreds or even thousands of headers to be (1) loaded and (2) compiled. Every one of them typically has to be recompiled for every compilation unit, because the preprocessor ensures that the result of compiling a header might vary between every compilation unit.


2 Answers

SysInternals ProcMon will show you all the I/O done by selected processes, including the timestamp when it happened and the full path.

like image 93
MSalters Avatar answered Oct 26 '22 12:10

MSalters


There are a few ways to optimize your compile time:

  • Use a precompiled header. If you use Visual C++ than use the "stdafx.h". By default "stdafx.h" is set to be the precompiled header but I would recommend you checking this to make sure it is.

  • Use Linux :). If you don't have to use Windows for what you are doing I recommend using Linux. The compilation times on it are a lot better. Some of the reasons are: ext4 is ~40% faster than NTFS on average and it has a better process scheduler. When it comes to processor operations, Linux usually does it ~x1.8 - x2 times faster and compiling is processor dependent.

  • Use another Compiler. Clang/LLVM is known to have better compilation speeds and also has support for precompiled headers.

like image 38
Lilian A. Moraru Avatar answered Oct 26 '22 12:10

Lilian A. Moraru