Skip to content

Instantly share code, notes, and snippets.

@hageldave
Created January 18, 2017 15:49
Show Gist options
  • Save hageldave/28508530cf02214e472fcddf75a0873b to your computer and use it in GitHub Desktop.
Save hageldave/28508530cf02214e472fcddf75a0873b to your computer and use it in GitHub Desktop.
performance test for Pixel with cached x,y values using a box blur with large neighbourhood (51x51 ~ 2600 calls to getX,getY per pixel)
public static void main(String[] args) {
Img img1 = new Img(4000, 4000);
Img img2 = new Img(4000, 4000) {
@Override
public Pixel getPixel() {
return new Pixol(this, 0);
}
@Override
public Pixel getPixel(int x, int y) {
return new Pixol(this, x, y);
}
};
img1.forEach(px->{px.setValue(0xff000000|px.getIndex());});
img2.forEach(px->{px.setValue(0xff000000|px.getIndex());});
BiConsumer<Pixel, Img> filter = (px, copy)->{
int size = 25;
int sum = 0;
for(int y = -size; y <= size; y++)
for(int x = -size; x <= size; x++){
sum += Pixel.getLuminance(copy.getValue(px.getX()+x, px.getY()+y, Img.boundary_mode_mirror));
}
sum /= (size+size+1)*(size+size+1);
px.setRGB(sum, sum, sum);
};
Img cp1 = img1.copy();
Img cp2 = img2.copy();
img1.setSpliteratorMinimumSplitSize(1024*32);
img2.setSpliteratorMinimumSplitSize(1024*32);
long t = System.nanoTime();
img1.forEachParallel(px->{filter.accept(px, cp1);});
t = System.nanoTime()-t;
System.out.println(t/1000.0);
t = System.nanoTime();
img2.forEachParallel(px->{filter.accept(px, cp2);});
t = System.nanoTime()-t;
System.out.println(t/1000.0);
ImageSaver.saveImage(img1.getRemoteBufferedImage(), "img1.png");
ImageSaver.saveImage(img2.getRemoteBufferedImage(), "img2.png");
}
static class Pixol extends Pixel {
private int xCached = -1;
private int yCached = -1;
public Pixol(Img img, int index) {
super(img, index);
}
public Pixol(Img img, int x, int y) {
super(img, x, y);
}
@Override
public int getX() {
return xCached == -1 ? (xCached = super.getX()):xCached;
}
@Override
public int getY() {
return yCached == -1 ? (yCached = super.getY()):yCached;
}
@Override
public void setIndex(int index) {
xCached = -1;
yCached = -1;
super.setIndex(index);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment