【问题标题】:Why does this code run so slowly?为什么这段代码运行得这么慢?
【发布时间】:2014-08-31 05:47:48
【问题描述】:

我正在尝试使用这些方法在屏幕上的矩形区域中查找颜色。然而,有时屏幕上有数百万像素,而我目前每秒只能实现大约 35 次 getColor 迭代。我的代码中一定有什么东西导致它运行得非常慢。

我怎样才能比这更快地扫描我的屏幕?理想情况下,我希望在不到一秒的时间内扫描整个屏幕的颜色,而不是现在的 8 小时:P

这是我的两种方法。

public static int getColor(int x, int y){
    try{
        return(robot.getPixelColor(x, y).getRGB() * -1);
    }catch(Exception e){
        System.out.println("getColor ERROR");
        return 0;
    }
}

//returns first instance of color,
//Begins top left, works way down to bottom right
public static Point findColor(Box searchArea, int color){
    System.out.println("Test");
    if(searchArea.x1 > searchArea.x2){
        int temp = searchArea.x1;
        searchArea.x1 = searchArea.x2;
        searchArea.x2 = temp;
    }
    if(searchArea.y1 > searchArea.y2){
        int temp = searchArea.y1;
        searchArea.y1 = searchArea.y2;
        searchArea.y2 = temp;
    }
    for(int i = searchArea.x1;i <=searchArea.x2; i++){
        for(int j = searchArea.y1;j<=searchArea.y2;j++){
            if(getColor(i, j) == color){
                return new Point(i, j);

            }
            System.out.println(i + " " + j);
        }
    }
    return new Point(-1, -1);
}

【问题讨论】:

  • 可能是因为它是递归函数?
  • 呃哦......我刚刚尝试了一个 for 循环到一百万,打印每个数字,但仍然需要整整一分钟才能完成。如果我的计算机 (AMD FX-6300) 甚至无法足够快地处理空白循环,我该怎么做?
  • not 8 hours as it stands now - 真的吗?
  • @ScaryWombat,我的方法永远不会在上面的代码中调用自身......是的,35 次迭代/秒 = 1 000 000 次迭代/8 小时
  • robot.getPixelColor 是瓶颈。获取屏幕截图并处理生成的BufferedImage 可能会更快

标签: java performance awt awtrobot


【解决方案1】:

Robot#getColor 会很慢,尤其是在以这种方式使用时。

更好的解决方案是抓取屏幕截图(即使是小块)并处理生成的BufferedImage

使用下面的例子我得到...

Took 0 seconds to scan image
Took 3 seconds to scan screen

对于10x10的区域

示例代码...

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestColorGrab {

    private static Robot robot;

    public static void main(String[] args) {
        try {
            robot = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(TestColorGrab.class.getName()).log(Level.SEVERE, null, ex);
        }
        new TestColorGrab();
    }

    public TestColorGrab() {
        Rectangle bounds = new Rectangle(0, 0, 10, 10);
        long start = System.currentTimeMillis();
        scanImageArea(bounds);
        System.out.println("Took " + ((System.currentTimeMillis() - start) / 1000), TimeUnit.SECONDS) + " seconds to scan image");

        start = System.currentTimeMillis();
        scanRobotArea(bounds);
        System.out.println("Took " + ((System.currentTimeMillis() - start) / 1000) + " seconds to scan screen");
    }

    public static int getColor(int x, int y) {
        try {
            return (robot.getPixelColor(x, y).getRGB() * -1);
        } catch (Exception e) {
            System.out.println("getColor ERROR");
            return 0;
        }
    }

    public static void scanRobotArea(Rectangle searchArea) {
        for (int i = searchArea.x; i < searchArea.x + searchArea.width; i++) {
            for (int j = searchArea.y; j < searchArea.y + searchArea.height; j++) {
                getColor(i, j);
            }
        }
    }

    public static void scanImageArea(Rectangle searchArea) {
        BufferedImage image = robot.createScreenCapture(searchArea);
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                image.getRGB(x, y);
            }
        }
    }

}

【讨论】:

  • 10500 秒扫描?我希望那是一个错字:P 不过,这应该会好很多,我会摆弄它,看看是否能达到我想要的效率水平,谢谢!
  • 31000 秒扫描 10x10?
  • 时间转换已关闭;)
  • 啊,好吧,0秒更有希望;)
  • 使用BufferedImage扫描整个屏幕 (2560x1600) 花了不到一秒钟的时间
【解决方案2】:

取出打印件。您正在向控制台写入一百万次。

【讨论】:

  • 扫描一个 50x50,用 println 用了 76 秒,不用用了 49 秒。我认为 MadProgrammer 发现了最大的问题,但很高兴知道我在测试效率时不能包含打印语句
猜你喜欢
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2017-01-10
  • 2011-08-31
相关资源
最近更新 更多