简体   繁体   English

如何在 CMake 中测量每个对象的编译时间?

[英]How to measure compile time per object in CMake?

I'm searching for tool that helps calculate compilation time using CMake.我正在寻找有助于使用 CMake 计算编译时间的工具。 Target project builds a lot of files, so I need to craft some "table" per each module.目标项目构建了很多文件,所以我需要为每个模块制作一些“表”。 For example, project consists of 2 files - main.cc and libs.cc I need to calculate build time for libs and main differently.例如,项目由 2 个文件组成 - main.cc 和 libs.cc 我需要以不同的方式计算 libs 和 main 的构建时间。

I'm trying to set wrapper for g++, but in make -j4 it has some "race condition".我正在尝试为 g++ 设置包装器,但在make -j4它有一些“竞争条件”。 Strings with Building status and building time are crossing.具有构建状态和构建时间的字符串正在交叉。

.......
[  3%] Building CXX object irohad/simulator/CMakeFiles/block_creator_common.dir/block_creator_common.o
0.12    1.42
[  4%] Building CXX object schema/CMakeFiles/yac_grpc.dir/yac.grpc.pb.o
0.13    1.36
0.10    0.71
.......

Set new "Compiler" - wrapper for g++设置新的“编译器”——g++ 的包装器

#!/bin/bash
{ /usr/bin/time -f "%S\t%U" g++ "$@"; } 2> >(cat <(echo "g++ $@") - | cat)

and then接着

cmake -H. -Bbuild -DCMAKE_CXX_COMPILER=/path/to/script.sh
make | tee -a buildResults.txt


Please try adding following lines in the cmake, to measure time you can compile with single cpu and then run in parallel mode 请尝试在cmake中添加以下行,以测量时间,您可以使用单个cpu进行编译,然后以并行模式运行

set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")

set_property(TARGET <your target> PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")

replace per your project name. 根据您的项目名称替换。

Hope this helps. 希望这可以帮助。

My solution is: 我的解决方案是:

#!/bin/bash
( /usr/bin/time -f "%S\t%U" g++ "$@" 2> >(cat <(echo "g++ $@") - )) | sponge

sponge allows to put output only after program exits 海绵仅在程序退出后才允许输出

你可以试试这个

set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time") set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CMAKE_COMMAND} -E time")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM