Effacer les filtres
Effacer les filtres

Avoid Copies of Arrays in MEX Functions

1 vue (au cours des 30 derniers jours)
Jan Kolar
Jan Kolar le 4 Jan 2022
Modifié(e) : Jan Kolar le 4 Jan 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 ?

Réponses (0)

Catégories

En savoir plus sur Deploy to C++ Applications Using mwArray API (C++03) dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by