I need to make a time profiling for several modules in Fortran, which means, that I'm supposed to write the same code in every beginning and every end of every function.
Really, it looks like this:
#ifdef TIME_PROFILING
real :: t1, t2 !< Variables used for time storage
call cpu_time(t1)
#endif
*** FUNCTION CODE ***
#ifdef TIME_PROFILING
call cpu_time(t2)
write(*, "(A)") '+----------------+'
write(*, "(A, E15.7, A)") '| TIME PROFILING | Function init_components() was running for ', t2-t1, 's.'
write(*, "(A)") '+----------------+'
#endif
I was thinking about creating function, which would contain calling cpu_time()
for the second time and the final message, but I don't think, it's enough. Still I'll be forced to write all the directives, to declare variables t1
and t2
and to call cpu_time()
manually for the first time in every function.
Initially I wanted to make a separate module time_profiler_m
for this function.
After thinking about disadvantages I came up with an idea of creating macros - that would allow me to write something like this:
#ifdef TIME_PROFILING
TIME_PROFILING_START
#endif
*** FUNCTION CODE ***
#ifdef TIME_PROFILING
TIME_PROFILING_STOP
#endif
which is much shorter, but, on the other hand, I'm not sure about correctness of using macros like this and I'm pretty sure, that it is a pretty bad design to create a separate module for one macro.
So, what do you think is the correct design in this situation?