我正在使用 perf 来分析 C++ 程序。代码如下:
std::string generateRandomString() {
// Initialize a random number generator
std::random_device rd;
std::mt19937 gen(rd());
// Define a distribution for the length of the string (1 to 6)
std::uniform_int_distribution<> lenDistribution(1, 6);
int length = lenDistribution(gen);
// Define characters that can be part of the string
const std::string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Generate the random string
std::uniform_int_distribution<> charDistribution(0, characters.size() - 1);
std::string result;
result.reserve(length);
for (int i = 0; i < length; ++i) {
result += characters[charDistribution(gen)];
}
return result;
}
void test_trie(){
for (...) generateRandomString();
}
void test(){
// do something
}
int main(){
test_trie();
test();
}
然后
g++ -g -O2 main.cpp -o secmaster_test
编译它(-O0
也尝试过)perf record -F 999 -g -- ./secmaster_test
但我认为结果应该如下:
| --- some random -----|
| ...something... | generateRandomString | -- --|
| ----------test-------- | --------------test_trie-----|
| ---------------------main----------------------------|
|--startup code-- | -------------------secmaster_test------------------- | -- cleanup -- |
似乎
perf
不知何故不知道在main()
中调用了有关随机的函数?
顺便说一句,我尝试过:
perf record --call-graph dwarf
perf record --call-graph fp
并且失败了