I am trying to time how long each file in my codebase takes to compile.
According to this SO answer, you can write a script which does times the actual compilation and then stores the results in a file
/tmp/time-build
:
#!/bin/bash
{ time g++ "$@"; } 2> >(cat <(echo "g++ $@") - >> /tmp/results.txt)
You can then override CMAKE_CXX_COMPILER
when calling cmake so that make uses the script to perform the compilation
cmake .. -DCMAKE_CXX_COMPILER=/tmp/time-build
make
This works as advertised, and yields results similar to the following:
real 0m1.190s
user 0m1.044s
sys 0m0.140s
g++ -Werror -Wall -Wextra ... /src/test/foo.cpp
However, to ease processing, I would like to store only the real
time, and have it on the same line as the g++ command.
Question:
My command line fu is not up to the task of turning this:
{ time g++ "$@"; } 2> >(cat <(echo "g++ $@") - >> /tmp/results.txt)
Into a command which captures only the real
output of time, and includes it along with the echo "g++ $@"
on a single line.
I don't know what the >(cat <(
and ) -
parts of the above command mean, and my attempts to incorporate a grep real
and echo
have failed
How can I do that?
Alternately, if there is a more idiomatic way to get cmake to output timings for each file processed, that too would be ideal.