// 执行多项计算任务的函数 voiddo_work(){ // 计算阶乘 for (int i = 1; i <= 20; i++) { unsignedlonglong result = factorial(i); } // 计算斐波那契数列(数值不太大,避免过长运行时间) for (int i = 1; i <= 30; i++) { unsignedlonglong result = fibonacci(i); } // 矩阵运算 int matrix_size = 100; auto A = create_matrix(matrix_size, 1.2); auto B = create_matrix(matrix_size, 0.8); auto C = create_matrix(matrix_size, 0.0); matrix_multiply(A, B, C); // 添加一些人为延迟 std::this_thread::sleep_for(std::chrono::milliseconds(10)); }
// 另一个函数,用于展示调用图 voidanother_function(){ // 进行一些计算 double sum = 0.0; for (int i = 0; i < 1000000; i++) { sum += i * 0.01; } // 调用do_work do_work(); }
intmain(){ std::cout << "Starting gprof example program\n"; // 多次调用函数 for (int i = 0; i < 5; i++) { do_work(); another_function(); } std::cout << "Program completed\n"; return0; }
Error: Access to performance monitoring and observability operations is limited. Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open access to performance monitoring and observability operations for processes without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability. More information can be found at 'Perf events and tool security' document: https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html perf_event_paranoid setting is 4: -1: Allow use of(almost) all events by all users Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK >= 0: Disallow raw and ftrace function tracepoint access >= 1: Disallow CPU event access >= 2: Disallow kernel profiling To make the adjusted perf_event_paranoid setting permanent preserve it in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)