Avoid Copies of Arrays in MEX Functions
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 ?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 ? 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 ? mex, performance, c++ MATLAB Answers — New Questions