I'd like to profile a mex function I've written in the matlab (2021a) editor. The best I can do right now is to using matlab's tic, toc functions to measure total execution time, but I dont know how to use more detailed diagnostic tools to evaluate the code performance. I've found other questions and responses discussing this issue by using visual studio, but the responses appear to use older versions of visual studio as opposed to my current one of 2019. I'm also not super familiar with visual studio, so I'm not sure where to find some of the tools they mention which appear to have been moved from where they were in previous versions.
1 Answers
There were two solutions I decided to pursue for this. Number one was adding timers to my code as suggested in the comments above (stackoverflow.com/a/47888078/7328782). You can then program your script to output the values to the matlab console using the following code snippet:
std::ostringstream stream;
stream << "Here is the name/value pair that you entered." << std::endl;
displayOnMATLAB(stream);
stream << SIG.rows() << " " << SIG.cols() << std::endl;
displayOnMATLAB(stream);
And make sure to include the following function definition outside the main body of the mexFunction but still inside the class definition of the mexFunction:
// outputting to matlab console for debugging
void displayOnMATLAB(std::ostringstream& stream) {
// Pass stream content to MATLAB fprintf function
matlabPtr->feval(u"fprintf", 0,
std::vector<matlab::data::Array>({ factory.createScalar(stream.str()) }));
// Clear stream buffer
stream.str("");
}
However, simply timing a piece of c++ code doesn't necessarily give a full picture of what you can optimize with your code. For example, Microsoft visual studio's profiling tools (Detailed introduction/overview/tutorials included here) can tell you how much memory is being consumed at a given line, your processor usage throughout the script and much more. You might be explicitly looking to get a good idea of how neat and efficient a certain line is, or you might catch that an innocuous line could be running fast relative to your bottleneck but is actually consuming far more resources than it should. I decided to simply port my mex function code over to visual studio. Luckily, Matlab support has published a response on their forums explaining how to do this. So far, it seems to be working with 2021a and Visual Studio 2019 even though the response was written for older versions of both Matlab and Visual Studio.
(Note, the module definition file only needs you to substitute the MYFILE statement with your project name I think. The other two lines should be left as written)
Update
The above answer has some issues to be worked out. The best option I've found thus far is using Cris Luengo's approach mentioned in the comment above.

- 524
- 1
- 8
-
As it turns out, this was not an entirely correct or complete answer. The best approach I've found thus far is still Cris Luengo's suggestion to use the standard library's timing functions and having a test version of your script where one of your outputs is a vector containing the timing data. I'm leaving the question open in case anyone finds better solutions. β drakon101 Dec 14 '21 at 00:20