Junior — Middle
How can one identify the part of the program that is the bottleneck and slows down the overall system performance?
sobes.tech AI
Answer from AI
Profiling is used to identify bottlenecks in a program — measuring the execution time of different parts of the code.
Main methods:
- Profilers: tools like gprof, Valgrind (Callgrind), perf allow collecting statistics on functions.
- Timing measurements: inserting timers (for example, using std::chrono in C++) around suspicious sections.
- Resource analysis: monitoring CPU, memory, input/output usage.
Example using std::chrono:
#include <iostream>
#include <chrono>
void functionToTest() {
// code
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
functionToTest();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Execution time: " << diff.count() << " seconds\n";
}
Thus, the most time-consuming sections are identified and optimized.