Working with heavy runtime profiling in C ++

I am currently working on a scientific computing project with huge data and complex algorithms, so I need to do a lot of code profiling. Currently, I rely on <ctime>and clock_ton the runtime of my code. I am completely satisfied with this solution ... except that I basically synchronize everything, and therefore for each line of real code I have to call start_time_function123 = clock(), end_time_function123 = clock()and cout << "function123 execution time: " << (end_time_function123-start_time_function123) / CLOCKS_PER_SEC << endl. This leads to heavy code bloat and quickly makes my code unreadable. How do you handle this?

The only solution I can think of is to find an IDE that allows me to mark parts of my code (in different places, even in different files), and toggle the hide / show all marked codes with one button. This would allow me to hide the part of my code related to profiling most of the time, and display it only when I want.

+6
source share
2 answers

Have an RAII type that marks the code as time.

struct timed {
  char const* name;
  clock_t start;
  timed( char const* name_to_record):
    name(name_to_record),
    start(clock())
  {}
  ~timed(){
    auto end=clock();
    std::cout << name << " execution time: " << (end-start) / CLOCKS_PER_SEC << std::endl;
  }
};

Use him:

void foo(){
  timed timer(__func__);
  // code
}

Far less noise.

. , , . cout esoecially endl ; , , . , , .

+7

, , - , .

, , , . , .

( ). , Ctrl-C, Ctrl-Break "" IDE. , , .

, , , - , .

, , , - , , , . , , .

, , . , , - . , , , , .

+1

Source: https://habr.com/ru/post/1015269/


All Articles