Why does my MATLAB C Math Library application run progressively slower?

8 次查看(过去 30 天)
I am writing a program that executes a loop and must run very quickly (each loop must be under
0.001 sec). The slowest part of a function is by far the line:
mlfAssign(&A, mlfDoubleMatrix(numSpikes, 4, listSpikes, NULL));
This line takes longer and longer each time the function is called.
The context of the function call is as follows:
func()
{
mxArray *A;
double listSpikes[..]
...
//Not inside a loop
mlfAssign(&A, mlfDoubleMatrix(numSpikes, 4, listSpikes, NULL));
...
mxDestroyArray(A);
}

采纳的回答

MathWorks Support Team
It is quite likely that the problem you are experiencing is due to memory leaks in your code. Therefore, you can either use the MATLAB C Math Library's automated memory management feature, or use the explicit memory management.
The explicit memory management requires more account keeping. Each time you allocate memory for an mxArray variable (e.g., when you use the mlfDoubleMatrix routine) you have to destroy the array using mxDestroyArray function. Therefore, if the allocation is done in a loop, the mxDestroyArray should also be called in the loop.
Furthermore, you will have to use the assignment operator "=" to assign values to an mxArray under explicit memory management otherwise you create a memory leak.
With the automated memory management , you have to make sure that:
  • the mxArray is initialized
  • the automated memory management feature is turned on by calling the mlfEnterNewContext and mlfRestorePreviousContext function pair.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by