ts=linspace(0,20,500000);
tic;
for i=1:1000
f=min(find(ts>10));
end;
toc
function [data,x,lower,upper]= find_halfspace_rec(data,x,lower,upper);
mp=floor((lower+upper)/2);
if mp~=lower
if data(mp)<x
[data,x,lower,upper]= find_halfspace_rec(data,x,mp,upper);
else
[data,x,lower,upper]= find_halfspace_rec(data,x,lower,mp);
end;
end;
function l = find_halfspace(data,x);
% returns the number of the last sample in the ASCENDING array data that is
% &lt; x using a simple half space search
upper = numel(data); lower = 1;
[data,x,l,u] = find_halfspace_rec(data,x,lower,upper);
ts=linspace(0,20,500000);
tic;
Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/
第 1 頁,共 3 頁 2015/3/9 下午 06:18
for i=1:1000
f=find_halfspace(ts,10);
end;
toc
#include "mex.h"
/*
* find_halfspace_mex.c
* same as find_halfspace.m
*/
int binary_search(double a[], int low, int high, double target[]) {
int result=-1;
while (low = a[middle])
low = middle + 1;
else
return middle;
result=middle;
}
return result+1;
}
void find_halfspace_mex(double y[], double x[],double f[],int low, int high)
{
y[0]=binary_search(x, low, high, f);
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *x,*y, *f;
size_t mrows,ncols,maxsize;
/* Check for proper number of arguments. */
if(nrhs!=2) {
mexErrMsgIdAndTxt( "MATLAB:find_halfspace_mex:invalidNumInputs", "two inputs required."
} else if(nlhs&gt;1) {
mexErrMsgIdAndTxt( "MATLAB:timestwo:maxlhs", "Too many output arguments.");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if (mrows&gt;ncols)
maxsize = mrows;
else
maxsize = ncols;
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
f = mxGetPr(prhs[1]);
y = mxGetPr(plhs[0]);
if ((mrows==0)||(ncols==0))
y[0]=-1;
else
find_halfspace_mex(y,x,f,0,maxsize);
Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/
第 2 頁,共 3 頁 2015/3/9 下午 06:18
}
ts=linspace(0,20,500000);
tic;
for i=1:1000
f=find_halfspace_mex(ts,10);
end;
toc
Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/
第 3 頁,共 3 頁 2015/3/9 下午 06:18

Fast lookup in sorted array jakob voigts

  • 1.
    ts=linspace(0,20,500000); tic; for i=1:1000 f=min(find(ts>10)); end; toc function [data,x,lower,upper]=find_halfspace_rec(data,x,lower,upper); mp=floor((lower+upper)/2); if mp~=lower if data(mp)<x [data,x,lower,upper]= find_halfspace_rec(data,x,mp,upper); else [data,x,lower,upper]= find_halfspace_rec(data,x,lower,mp); end; end; function l = find_halfspace(data,x); % returns the number of the last sample in the ASCENDING array data that is % &lt; x using a simple half space search upper = numel(data); lower = 1; [data,x,l,u] = find_halfspace_rec(data,x,lower,upper); ts=linspace(0,20,500000); tic; Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/ 第 1 頁,共 3 頁 2015/3/9 下午 06:18
  • 2.
    for i=1:1000 f=find_halfspace(ts,10); end; toc #include "mex.h" /* *find_halfspace_mex.c * same as find_halfspace.m */ int binary_search(double a[], int low, int high, double target[]) { int result=-1; while (low = a[middle]) low = middle + 1; else return middle; result=middle; } return result+1; } void find_halfspace_mex(double y[], double x[],double f[],int low, int high) { y[0]=binary_search(x, low, high, f); } void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *x,*y, *f; size_t mrows,ncols,maxsize; /* Check for proper number of arguments. */ if(nrhs!=2) { mexErrMsgIdAndTxt( "MATLAB:find_halfspace_mex:invalidNumInputs", "two inputs required." } else if(nlhs&gt;1) { mexErrMsgIdAndTxt( "MATLAB:timestwo:maxlhs", "Too many output arguments."); } /* The input must be a noncomplex scalar double.*/ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if (mrows&gt;ncols) maxsize = mrows; else maxsize = ncols; /* Create matrix for the return argument. */ plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); /* Assign pointers to each input and output. */ x = mxGetPr(prhs[0]); f = mxGetPr(prhs[1]); y = mxGetPr(plhs[0]); if ((mrows==0)||(ncols==0)) y[0]=-1; else find_halfspace_mex(y,x,f,0,maxsize); Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/ 第 2 頁,共 3 頁 2015/3/9 下午 06:18
  • 3.
    } ts=linspace(0,20,500000); tic; for i=1:1000 f=find_halfspace_mex(ts,10); end; toc Fast lookupin sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/ 第 3 頁,共 3 頁 2015/3/9 下午 06:18