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.