Azzera filtri
Azzera filtri

Avoid Copies of Arrays in MEX Functions

1 visualizzazione (ultimi 30 giorni)
Jan Kolar
Jan Kolar il 4 Gen 2022
Modificato: Jan Kolar il 4 Gen 2022
My question is about following example which can be found in documentation:
#include "mex.hpp"
#include "mexAdapter.hpp"
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function {
ArrayFactory factory;
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
double sm = 0;
const TypedArray<double> inArray = inputs[0];
for (auto& elem : inArray) {
sm += elem;
}
outputs[0] = factory.createScalar(sm);
}
};
How important is to use the keyword "const" in declaration of inArray ? Will there be performance difference if I omit it ? I use following function in MEX module which is called from MexFunction::operator():
void Initialize(TypedArray<double> x, TypedArray<double> y)
{
...
}
void operator()(ArgumentList outputs, ArgumentList inputs)
{
...
Initialize(inputs[1], inputs[2])
...
}
Should I declare the input arguments of Initialize function as const to improve performance ? Problem is that code can be compiled and run without problems but I don't know what happens under the hood. If possible I want to avoid uneccessary allocations and deallocations. Especially if it is that simple like adding one keyword. In other projects unrelated to MATLAB i use references or pointers where possible if the underlying data type is complex. What is preferred way to pass data in MEX modules ?

Risposte (0)

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by