0

Built this simple program that just tracks when one pixel changes to white and records the delay + 100ms. Execution time of the simple task on a i9 workstation desktop with high specs returns a delay of 11ms. What can I do to achieve maybe a delay of 4ms since I have to loop this task and trigger constantly to model a stopwatch.

    import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;
import java.util.concurrent.TimeUnit;

public class ColorPickerDemo {


    public static void main(String[] args) throws AWTException, InterruptedException {

            // Data Storage Array
            long[] rotateTime = new long[50];

            Robot robot = new Robot();

            // Queue time start
            long startTime = System.nanoTime();

            // Count of Array Length
            int count = 0;

            // Semi-Infinite Loop
            while (count < 50) {

                // Find the pixel color information at Position
                Color colorA = robot.getPixelColor(889, 314);
                //Color colorB = robot.getPixelColor(886, 319);

                    // Tell IF its white/tan colour
                    if (colorA.getRed() > 95 &&
                        colorA.getGreen() > 80 &&
                        colorA.getBlue() > 65)
                        {

                        // Record Time
                        long endTime = System.nanoTime();
                        long totalTime = endTime - startTime;

                        // Data storage
                        rotateTime[count] = totalTime;

                        // Print the RGB information of the pixel color
                        // System.out.println("Time  = " + (totalTime/1000000) );
                        // System.out.println("RGB = " + colorA.getRed() + ", " + colorA.getGreen() + ", " + colorA.getBlue());

                        // Reset the timer & add count
                        startTime = System.nanoTime();
                        count ++;

                        Thread.sleep(100);

                    }




            }

            for (int x = 0; x < rotateTime.length; x++) {
                System.out.println("index " + x + " : "+ ( rotateTime[x] / 1000000 ) );
            }
    }
}
jon_woot
  • 21
  • 3

1 Answers1

0

Rather than managing time yourself, why don't you run your program with gprof and see how much time each execution of pixel change takes.

vavasthi
  • 922
  • 5
  • 14