I have created 2 little benchmarks:
Code:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Robot r;
try {
for(int i = 0; i < 1000000; i++){
int b = i*i;
System.out.println(i);
}
r = new Robot();
System.out.println("Start");
long startTime = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
Color c = r.getPixelColor(100, 100);
}
System.out.println("Time: " + (System.currentTimeMillis() - startTime)*0.001 + "ms per iteration");
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Time: 22.141000000000002ms per iteration
Code:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Robot r;
try {
for(int i = 0; i < 1000000; i++){
int b = i*i;
System.out.println(i);
}
r = new Robot();
System.out.println("Start");
long startTime = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
BufferedImage image = r.createScreenCapture(new Rectangle(600, 400));
image.getSubimage(0,0,20,20);
image.getSubimage(0,0,20,20);
image.getSubimage(0,0,20,20);
}
System.out.println("Time with Subimage: " + (System.currentTimeMillis() - startTime)*0.001 + "ms per iteration");
startTime = System.currentTimeMillis();
for(int i = 0; i < 1000; i++){
r.createScreenCapture(new Rectangle(20, 20));
r.createScreenCapture(new Rectangle(20, 20));
r.createScreenCapture(new Rectangle(20, 20));
}
System.out.println("Time without Subimage: " + (System.currentTimeMillis() - startTime)*0.001 + "ms per iteration");
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Time with Subimage: 21.661ms per iteration
Time without Subimage: 87.92ms per iteration
The results are a little strange, but I think we can see, that its better to take a bigger screenshot in java and to process that image, instead of taking many small screenshots as many do.
Checking for changed pixels with getPixelColor() Also has no benefits over just taking a screenshot.