使用 MSVC 获取预处理源代码的最快方法是什么?
我试图找到最快的方法来获取 C 源文件的完整预处理源代码(我不需要 #line 信息其他注释,只需要原始源代码)。
我有以下小测试程序,其中仅包含 Windows 头文件 (mini.c
):
#include <windows.h>
使用 Microsoft Visual Studio 2005,然后运行此命令:
cl /nologo /P mini.c
生成 2.5MB mini 需要 6 秒。我归档;将其更改为
cl /nologo /EP mini.c > mini.i
(跳过注释和 #line 信息)只需 0.5 秒即可写入 2.1MB 的输出。
有人知道在不使用预编译头的情况下进一步改进这一点的好技术吗?
我问这个问题的原因是我为 MSVC 编写了流行的 ccache 工具的变体。作为工作的一部分,程序需要计算预处理源代码(以及其他一些东西)的哈希和。我想让这件事尽快完成。
也许有一个专用的预处理器二进制文件可用,或者其他可能有帮助的命令行开关?
更新:我刚刚想到的一个想法:定义WIN32_LEAN_AND_MEAN 宏来删除大量很少需要的代码。这将上述预处理器的运行速度提高了大约 3 倍。
I'm trying to find the fastest way to get the complete preprocessed source code (I don't need #line information other comments, just the raw source code) for a C source file.
I have the following little test program which just includes the Windows header file (mini.c
):
#include <windows.h>
Using Microsoft Visual Studio 2005, I then run this command:
cl /nologo /P mini.c
This takes 6 seconds to generate a 2.5MB mini.i file; changing this to
cl /nologo /EP mini.c > mini.i
(which skips comments and #line information) needs just 0.5 seconds to write 2.1MB of output.
Is anybody aware of good techniques for improving this even further, without using precompiled headers?
The reason I'm asking is that I wrote a variant of the popular ccache tool for MSVC. As part of the work, the program needs to compute a hash sum of the preprocessed source code (and a few other things). I'd like to make this as fast as possible.
Maybe there is a dedicated preprocessor binary available, or other command line switches which might help?
UPDATE: One idea which just came to my mind: define the WIN32_LEAN_AND_MEAN macro to strip out lots of rarely needed code. This speeds the above preprocessor run up by a factor of approx 3x.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您重复处理同一个源文件 (
),这又会拉入许多其他文件。此
位于默认 SDK 目录中。现在,处理这个不变的文件需要花费大量时间。然而你可以相信它不会改变——毕竟它是公共界面的一部分。因此,您可以对其进行预处理 - 例如删除注释 - 并将该版本传递给 cl /EP 。
当然,这通常是 I/O 密集型任务,但混合了重要的 CPU 部分。并行处理多个源的方法将有助于提高总吞吐量。测量单源预处理时间并不是太相关。
最后,测量将输出写入
NUL
的时间。您不应该包括将mini.i
写入磁盘所需的时间,因为您打算将输出通过管道传输到md5sum
。You're repeatedly processing the same source file (
<windows.h>
) which in turn pulls in a lot of other files. This<windows.h>
is located in the default SDK directory.Now, processing this unchanging file takes serious time. Yet you can rely on it not changing - it's part of the public interface, after all. Hence you could preprocess it - strip out comments, for instance - and pass that version to
cl /EP
.Of course, this is typically an I/O bound task, but with a significant CPU part intermixed. An approach which processes multiple sources in parallel will help the total throughput. Measuring single source preprocesing times isn't too relevant.
Finally, measure the time to write the output to
NUL
. You shouldn't be including the time needed to writemini.i
to disk, since you'll intend to pipe the output tomd5sum
.您可以使用文件项目设置的预处理。此外,一些较新的 MSVC 版本提供多线程编译。
There's a preprocess to file project setting that you can use. In addition, some of the newer MSVC versions offer multithreaded compilation.
/P 选项已存在多年,它创建 .i 文件,但不创建目标文件。
The /P option has been there for years, it creates the .i file, and no object file.