MEX Code in Matlab Wrapper
조회 수: 6 (최근 30일)
이전 댓글 표시
I have the following code:
for i=1:N,
some_mex_file();
end
My MEX file does the following:
- Declares an object, of a class I defined, that has 2 large memory blocks, i.e., 32x2048x2 of type double.
- Processes the data in this object.
- Destroys the object.
I am wondering if it takes more time when I call a MEX file in a loop that allocates large memory blocks for its object. I was thinking of migrating to C++ so that I can declare the object only once and just reset its memory space so that it can be used again and again without new declaration. Is this going to make a difference or going to be a worthless effort? In other words, does it take more time to allocate a memory in MEX file than to declare it once and reuse it?
댓글 수: 0
답변 (1개)
James Tursa
2012년 7월 1일
Yes, it does take extra time to repeatedly do the allocation/deallocation. In your current code, is this a C++ object? A MATLAB object? You can create it at the highest level, i.e. outside the mexFunction so that it can be permanent. Then register a mexAtExit function to free it when the mex function gets cleared. Or, alternatively, if it is a MATLAB object you can create it once outside the loop at the m-file level, pass that into the mex function, and operate on it in-place. But that can get tricky because of data sharing among variables, so you would need to avoid that.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!