A
a0a
Hi everyone,
I did a little benchmark on the speedup that JAI can bring in
computing the point with minimum distance from a given source point
(x,y) out of an array of x,y coordinates.
I did this both with JAI and by brute-forcing it using a simple
distance check. Although the JAI is very elegant in that it
automatically returns ALL minimum distance points, it is also 6 times
slower than the brute-force method.
I am guessing that I may have gotten something wrong in the set-up,
but since the information on JAI and tutorials are rather scarce or
not working at all, I thought maybe someone here could take a look at
this:
------ this is what I want to do ------
ArrayList<Integer> indexes = new ArrayList<Integer> ();
int closestPoint = Integer.MAX_VALUE;
for (int i = 0; i < xvalues.length; i++)
{
int xdiff = xvalues - x;
int ydiff = yvalues - y;
int sqr = xdiff* xdiff + ydiff*ydiff;
if ( sqr == closestPoint )
{
indexes.add(i);
}
if ( sqr < closestPoint )
{
closestPoint = sqr;
indexes.clear();
indexes.add(i);
}
}
------ this is what does it, but 6 times slower, in JAI ------
double[] xSub = new double[]{x};
double[] ySub = new double[]{y};
RenderedOp renderedOpXSubXRef = SubtractConstDescriptor.create(imageX,
xSub, rh);
RenderedOp renderedOpYSubYRef = SubtractConstDescriptor.create(imageY,
ySub, rh);
RenderedOp renderedOpXDiff2 =
MultiplyDescriptor.create(renderedOpXSubXRef, renderedOpXSubXRef, rh);
RenderedOp renderedOpYDiff2 =
MultiplyDescriptor.create(renderedOpYSubYRef, renderedOpYSubYRef, rh);
RenderedOp renderedOpSum = AddDescriptor.create(renderedOpXDiff2,
renderedOpYDiff2, rh);
RenderedOp renderedOpExtrema = ExtremaDescriptor.create(renderedOpSum,
null, 1, 1, true, 1, rh);
List minLocations =
((List[])renderedOpExtrema.getProperty("minLocations"))[0];
------------------------------------------------------------------------------------
Of course, right before this code I convert the 2 int[] arrays
containing x values and y values into BufferedImages using a GrayScale
colorspace, 32 bit colorModel with no alpha, no transparency, no
premultiplication. I do not alter the rasters during computation, I
merely fetch the min values from the list "minLocations" afterwards.
Timing it indicates that processing is extremely slow, however, I
still have to rewrite the benchmark to warm up first and redo the test
a number of times, maybe the test may show improvement. However, if
anyone has ideas how to speed up the operation pipe, I am all ears!
I did a little benchmark on the speedup that JAI can bring in
computing the point with minimum distance from a given source point
(x,y) out of an array of x,y coordinates.
I did this both with JAI and by brute-forcing it using a simple
distance check. Although the JAI is very elegant in that it
automatically returns ALL minimum distance points, it is also 6 times
slower than the brute-force method.
I am guessing that I may have gotten something wrong in the set-up,
but since the information on JAI and tutorials are rather scarce or
not working at all, I thought maybe someone here could take a look at
this:
------ this is what I want to do ------
ArrayList<Integer> indexes = new ArrayList<Integer> ();
int closestPoint = Integer.MAX_VALUE;
for (int i = 0; i < xvalues.length; i++)
{
int xdiff = xvalues - x;
int ydiff = yvalues - y;
int sqr = xdiff* xdiff + ydiff*ydiff;
if ( sqr == closestPoint )
{
indexes.add(i);
}
if ( sqr < closestPoint )
{
closestPoint = sqr;
indexes.clear();
indexes.add(i);
}
}
------ this is what does it, but 6 times slower, in JAI ------
double[] xSub = new double[]{x};
double[] ySub = new double[]{y};
RenderedOp renderedOpXSubXRef = SubtractConstDescriptor.create(imageX,
xSub, rh);
RenderedOp renderedOpYSubYRef = SubtractConstDescriptor.create(imageY,
ySub, rh);
RenderedOp renderedOpXDiff2 =
MultiplyDescriptor.create(renderedOpXSubXRef, renderedOpXSubXRef, rh);
RenderedOp renderedOpYDiff2 =
MultiplyDescriptor.create(renderedOpYSubYRef, renderedOpYSubYRef, rh);
RenderedOp renderedOpSum = AddDescriptor.create(renderedOpXDiff2,
renderedOpYDiff2, rh);
RenderedOp renderedOpExtrema = ExtremaDescriptor.create(renderedOpSum,
null, 1, 1, true, 1, rh);
List minLocations =
((List[])renderedOpExtrema.getProperty("minLocations"))[0];
------------------------------------------------------------------------------------
Of course, right before this code I convert the 2 int[] arrays
containing x values and y values into BufferedImages using a GrayScale
colorspace, 32 bit colorModel with no alpha, no transparency, no
premultiplication. I do not alter the rasters during computation, I
merely fetch the min values from the list "minLocations" afterwards.
Timing it indicates that processing is extremely slow, however, I
still have to rewrite the benchmark to warm up first and redo the test
a number of times, maybe the test may show improvement. However, if
anyone has ideas how to speed up the operation pipe, I am all ears!