-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_profiler.sh
More file actions
executable file
·48 lines (35 loc) · 977 Bytes
/
run_profiler.sh
File metadata and controls
executable file
·48 lines (35 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
if [ "$#" -lt 1 ]; then
echo "Usage: $0 file"
exit 1
fi
SRC=$1
BIN=run_binary
PROFILE=cpu.prof
GP=$(brew --prefix gperftools 2>/dev/null)
if [ ! -d "$GP" ]; then
echo "gperftools not found. Install with: brew install gperftools"
exit 1
fi
now_ns() { date +%s%N; }
ns_to_s() { awk "BEGIN { printf \"%.6f\", $1 / 1e9 }"; }
compile_start=$(now_ns)
clang++ -std=c++23 -O3 -g \
-I"$GP/include" \
-L"$GP/lib" \
-lprofiler \
"$SRC" -o "$BIN"
compile_status=$?
compile_end=$(now_ns)
[ $compile_status -ne 0 ] && exit 1
echo "Compilation time: $(ns_to_s $((compile_end-compile_start))) s"
export CPUPROFILE=$PROFILE
export CPUPROFILE_FREQUENCY=1000
run_start=$(now_ns)
./"$BIN"
run_end=$(now_ns)
unset CPUPROFILE CPUPROFILE_FREQUENCY
echo "Compilation time: $(ns_to_s $((compile_end-compile_start))) s"
echo "Execution time: $(ns_to_s $((run_end-run_start))) s"
echo "Profile written to $PROFILE"
echo "View with: pprof ./$BIN $PROFILE"