assassin
assassin

Reputation: 21189

suppressing mexPrintf in matlab mex code

I have mex code that I call from my matlab scripts. To debug the code, I had put a lot of mexPrintf statements, but for timing purposes now I don't want any I/O taking place in my mex code call (since I/O takes a lot of time). What's the easiest and best way to suppress the mexPrintf calls in my code, such that those statements are not executed at all, without having to delete/comment out those statements? (I don't want to delete/comment out those statements and recompile my mex code because I may need these debug statements later on, and I don't want to keep going through this iteration of modifying and building my code again and again).

Is there any compiler switch that can do the trick? or some preprocessor statement?

Thanks!

Upvotes: 0

Views: 1275

Answers (1)

angainor
angainor

Reputation: 11810

You can not turn mexPrintf off. You need to modify your code. Define e.g. DEBUG flag to decide, when you want to print things, and when not. For example, with normal printf function

#include <stdio.h>
#include <stdlib.h>

//#define DEBUG
#ifdef DEBUG
#define MPRINT(...) printf(__VA_ARGS__);
#else
#define MPRINT(...)
#endif

int main()
{
  MPRINT("%d\n", 5);
}

Nothing is printed if you run it now. But if you uncomment the #define DEBUG statement, you get 5 printed.

Alternatively, you can embrace all mexPrintf calls in such clauses:

#ifdef DEBUG
mexPrintf(...);
#endif

Again, nothing will be printed if DEBUG is not defined. But that is much more work.

You can also do a similar thing without recompiling your mex file by using a normal if statement and pass a verbose parameter to the mex file. However, this will still have some impact on performance if you execute the if statement too often. So go for the DEBUG more - that is the standard way to do it.

Upvotes: 4

Related Questions