Reputation: 1128
I often optimize some matlab routines by implementing them in mex.
This works fine so far - but whenever creating the return mxArray
, it gets preinitialized as I build it with mxCreateNumericArray
.
In most of my work, I do not need the memory to be preallocated (zeroed). So sad - I can not find a matching command for that purpose (allocating memory without zeroing).
So my question could be answered in two ways:
mxArray
without zeroing the contents?mxArray
without allocating memory for Pr
(and Pi
)-field, which I would later allocate by mxMalloc
.One idea is building the Array with size (0,0) - but is this the ultimate solution?
Upvotes: 3
Views: 761
Reputation: 1128
With those lines from sebastian I played around. I think, the following lines show the answer to my question.
In words - even with zeroing the space the result is fast.
With my current application I tested four different scenarios:
plhs[0] = mxCreateNumericArray(targetDimCount, targetDims, targetClass, mxREAL);
plhs[0] = mxCreateNumericArray(0, 0, targetClass, mxREAL); mxSetData(plhs[0],mxMalloc(destLen_in_Bytes));
plhs[0] = mxCreateNumericArray(0, 0, targetClass, mxREAL); mxSetData(plhs[0],mxRealloc(mxGetData(plhs[0]),destLen_in_Bytes));
plhs[0] = mxCreateUninitNumericArray(targetDimCount, targetDims, targetClass, mxREAL);
resulting in those timings (for allocated matrices of size given in column one)
size time 1 time 2 time 3 time 4
1000 0.037401 0.037263 0.039294 0.037628
2000 0.14906 0.14937 0.15278 0.14917
3000 0.33497 0.33449 0.34601 0.33749
4000 0.61207 0.60546 0.61563 0.60086
5000 0.94057 0.93076 0.96147 0.95723
6000 1.3497 1.3475 1.3794 1.3559
7000 1.837 1.8265 1.8776 1.846
8000 2.398 2.3893 2.4625 2.3885
9000 3.0264 3.047 3.1374 3.0339
10000 3.7658 3.7677 3.8392 3.7862
20000 15.208 14.968 15.404 15.143
30000 13.583 13.58 13.955 13.648
50000 13.291 13.236 13.535 13.478
With a view on these numbers, my opinion is, that reallocating is a bad idea (but not so bad, as the timing shows) and zeroing the values is not a very big efford. Okay - one can further wonder on the behaviour between 10000 to 30000 - but that would be more scientific. Using malloc (only for testing purposes) is not working, as the allocated storage needs to be for a plhs - and it crashes unless the storage is allocated by mx***
Upvotes: 2
Reputation: 18504
Another possible option might be to us the undocumented function mxFastZeros
. It's one of many undocumented mex functions. You can read about it's use in this Matlab Central answer. I'd be interested to know if it does anything different that just creating a zero size matrix.
Upvotes: 1
Reputation: 9696
I guess, building a size (0,0) matrix is as close as it gets to a kind of "empty" constructor.
Afterwards you'll have to resize the array using mxMalloc
or mxRealloc
.
I doubt this will result in a noticable performance gain though, unless you're handling really large arrays.
Upvotes: 2