Implement skewness in C-mex
mex -O -R2018a skewness_mex.c
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
double *x, delta, a, c, M1, M2, M3, skewness;
M3 += a * (c * (n - 1) - 3 * M2);
skewness = M3 / (M2 * sqrt(M2/N));
plhs[0] = mxCreateDoubleScalar(skewness);
For both MSVS and Intel OneAPI compilers, as I have anticipated it is slower than MATLAB equivalent code, see timings in the code comments.
It shows that MATLAB EE is getting very good.
Your friend in Stackoverflow can come here and see the result.
For those who are on Windows and want to try the mex, pleas rename the attached mat file with .mexw64 extension. I do it to trick Answer.
timeseries = randi(1000, 1, n);
t_org = timeit(@()skewness_onepass(timeseries),1)
t_Bruno = timeit(@()skewness_onepass_BLU2(timeseries),1)
t_mex = timeit(@()skewness_mex(timeseries),1)
function skewness = skewness_onepass(x)
M3 = M3 + delta * delta * delta * (n - 1) * (n - 2) / (n * n) - 3 * delta * M2 / n;
M2 = M2 + delta * delta * (n - 1) / n;
skewness = (M3 / N) / (std_dev * std_dev * std_dev);
function skewness = skewness_onepass_BLU2(x)
M3 = M3 + a * (c * (n - 2) - 3 * M2);
skewness = M3 / (M2 * sqrt(M2/N));