I asked myself if there is a nice way to get different versions of one function without copying the whole source code. I would like to have different versions one the one hand for measuring the execution time and on the other hand for writing some intermediate result (for analytic purpose).
Let me explain it by this example: I am writing a iterative solver for solving a linear system, in my case the GMRES algorithm. It would like to measure the runtime in one run and to write the residual at every iteration step in a different run. The structure of my program looks like:
//main.c
#include "gmres.h"
// setup arrays
flag_print_res = 0;
GMRES(A,x,b,tol,maxtiter,flag_print_res);
// reset arrays
flag_print_res = 1;
GMRES(A,x,b,tol,maxtiter,flag_print_res);
//..
and the function:
//GMRES.c
void GMRES(double* A, double *x, double *b, double tol, int maxiter, int flag_print_res)
{
// …
start_time = ((double) clock ())/CLOCKS_PER_SEC;
for(iter=0; iter<maxiter; iter++)
{
// ...
if(flag_print_res)
{
fprintf(file, "%d %13.5e\n", iter, res);
}
// ...
}
end_time = ((double) clock ())/CLOCKS_PER_SEC;
printf("execution time = %13.5e\n", end_time-start_time);
}
However, the problem for the execution time is, that evaluation the if statement costs time, specially when it is evaluated every iteration. Therefore my question: How can I create different version of the function, in which the if statement is evaluated at compile time?
Use directives. With a directive you can instruct the C pre-processor to either print or not to print.
It is a common practice for debugging by the way. You can, at compile time, have only one decision being made. (You see below I put
#define
and#undef
in the same source file. The decision is at compile time, so you can use either one or the other. I hope you get the idea. You need to compile twice if you want to execute the two version of your program.)When invoking your program you can add a directive as parameter to the (gcc?) compiler, so you don't need to modify your source code every time you want to switch the directive (e.g.
gcc -D FLAG_PRINT_RES ....
). Below is showing the two poossibilities.And make use of the flag in case it is defined.
Note that it is a pre-processor (thus before compile time decision). You cannot have a dynamic approach at runtime in C without using function pointers, at least not as far as I know.
If you'd use C++ you might find a run time solution (you can overload methods/functions).
If you allow to change your code more or less considerably, you might be able to find a solution in C using function pointer. However, it is too much effort for me to set up an example, I'll show you the basics in the Appendix below. And I am not sure if it is better than the
if
statement, because each time a function is called a context is built and destroyed after return. However if you add more print statements then a function pointer could be a viable solution for you.So here is the summary:
if
statementAppendix (function pointer)