每个#include 指令预处理器的时间成本是多少?

What is the time cost for the preprocessor for each #include directive?

背景

C and C++, header files are files that allow class definitions (as well as other entities) to be the same between files. When another file requires facilities from these header files, the #include中以三种方式之一使用:

#include <stdio.h>         // Include the contents of the standard header 
                           // 'stdio.h' (probably a file 'stdio.h').
#include <vector>          // Include the contents of the standard header 
                           // 'vector' (probably a file 'vector.h').
#include "user_defined.h"  // Include the contents of the file 'user_defined.h'.

在每种情况下,预处理器都会将头文件中的文本复制到 #include 所在的文件中。鉴于头文件的普遍流行,我很好奇添加头文件包含的复制粘贴方法的时间。因此我的问题...

问题:预处理器用文本替换它遇到的所有 #include 指令的时间成本是多少?

从概念上讲,pre-processor 是编译 C 代码的一个单独的独立步骤。处理预处理指令所需的原始时间将因编译器和 OS 而异。更改处理搜索顺序或将 "post-preprocessor" 输出作为单独文件输出的编译器选项也会更改所需的时间。

您将主要受 I/O 时间的限制,因为即使使用 SSD,processor/main 内存也比 I/O 快得多。 I/O 处理将采用两种形式;搜索 header(s) 并读取 header 文件。复杂的宏扩展也可能需要时间,但同样由于 I/O 和主内存速度的相对差异,这通常比实际查找和读取 header 文件要次要。

大多数编译器将允许您生成预处理器输出而无需完全编译为二进制文件,而且大多数 OS 都有监视或分析 processes/commands 的方法。您可以使用这些功能大致了解使用的时间以及如何使用 CPU/memory 和 I/O。