0
For what it's worth, here's one that's just a few macros:#include <time.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define PRINTTIME printf( "%6.3f seconds used by the processor.", ((double)stopm-startm)/CLOCKS_PER_SEC);
Then just use it with:main() {
START;
// Do stuff you want to time
STOP;
PRINTTIME;
}
Not a good use of the preprocessor. Let's look at a few reasons why:
- Global variables used in macros. Scary.
- Object macros that have hidden side effects and don't really evaluate to objects or values.
- Order is important, but neither enforced nor encouraged (try doing PRINTTIME before START or STOP).
- Failure terminates the whole application!?
Seriously, just put forth a little more effort and write a stopwatch ADT. Macro hell isn't a good place to be.