构建一个简单的程序,该程序仅跟踪一个像素何时变为白色并记录延迟+ 100ms。在具有较高规格的i9工作站桌面上执行简单任务的时间将返回11ms的延迟。由于必须循环执行此任务并不断触发以对秒表建模,因此我该怎么办才能实现4毫秒的延迟。
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 ) );
}
}
}
答案 0 :(得分:0)
为什么不自己管理时间,为什么不使用gprof运行程序并查看每次执行像素更改需要花费多少时间。