Cookie Cookie - 2 months ago 15
C++ Question

Exclude parts from compilation - still syntax-check

We have a pretty large project here in C++/.NET/Visual Studio, for performance testing we incuded some code that makes it possible to evaluate the execution time between certain points.

PERFORMANCE_TEST BeginMeasureTime("execute someCode")
someCode
PERFORMANCE_TEST EndMeasureTime("execute someCode")


Usually while developing the PERFORMANCE_TEST macro would be set to
//
so the evaluation code is switched off. So if code / interfaces etc. change and the code after the
PERFORMANCE_TEST
macro becomes invalid, the developer will not notice that.

Is there any smarter way to have the performance evaluation code only built in a special version of the project but still make sure that it stays consistent? How would you normally do something like that?

Answer

One easy way would be to change

PERFORMANCE_TEST xxx

to

PERFORMANCE_TEST(xxx)

Then instead of

#if PERFORMANCE_TEST_ENABLED
#define PERFORMANCE_TEST
#else
#define PERFORMANCE_TEST //
#endif

you could have

#if PERFORMANCE_TEST_ENABLED
#define PERFORMANCE_TEST(x) {x;}
#else
#define PERFORMANCE_TEST(x) if(0) { x; }
#endif

An optimizing compiler should generate no code for the second version if performance tests are disabled (or at the very least it'll not branch on the if), but it'll still be part of the compilation.

Alternatively, if build times aren't a huge concern you could simply always build both versions into separate directories.

Comments