[英]How can I profile a MEX function written using the matlab editor and compiled using gcc
我決定為此采取兩種解決方案。 第一個是在我的代碼中添加計時器,如上面評論中的建議(stackoverflow.com/a/47888078/7328782)。 然后,您可以使用以下代碼段將您的腳本編程到 output 到 matlab 控制台的值:
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);
並確保在 mexFunction 的主體之外包含以下 function 定義,但仍在 mexFunction 的 class 定義內:
// 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("");
}
但是,僅對一段 c++ 代碼進行計時並不一定能全面了解您可以使用代碼優化什么。 例如,Microsoft Visual Studio 的分析工具( 此處包含詳細介紹/概述/教程)可以告訴您在給定的行中消耗了多少 memory、您在整個腳本中的處理器使用情況等等。 您可能明確地希望了解某條線路的整潔和高效程度,或者您可能會發現一條無害的線路相對於您的瓶頸可能運行得很快,但實際上消耗的資源比它應該消耗的多得多。 我決定簡單地將我的 mex function 代碼移植到 Visual Studio。 幸運的是,Matlab 支持已經在他們的論壇上發布了回復,解釋了如何做到這一點。 到目前為止,即使響應是針對 Matlab 和 Visual Studio 的舊版本編寫的,它似乎也適用於 2021a 和 Visual Studio 2019。
(注意,模塊定義文件只需要您將 MYFILE 語句替換為您的項目名稱即可。其他兩行應保持原樣)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.