gpt4 book ai didi

java - 计算按下按钮到执行一行代码之间的延迟的方法

转载 作者:行者123 更新时间:2023-12-01 10:08:22 24 4
gpt4 key购买 nike

是否可以在 Java/JavaFX 中评估按下键盘按钮和执行代码行之间的延迟?想象一下这样的情况,我向用户呈现一个刺激,其任务是当他或她感知到该刺激时按空格键。如何最准确地获得显示刺激和敲击键盘按键之间的延迟?

到目前为止,我已经使用了这样的东西:

long start_time = System.currentTimeMillis();
displayTheStimulus();

long end_time;
if (spacebarIsHitted()) {
end_time = System.currentTimeMillis();
} else {
end_time = start_time;
}

long difference = end_time-start_time

我如何确定此方法给出的值有效且准确?有没有办法评估这个方法的准确性?

最佳答案

根据java System根据文档,您可以使用 System.nanoTime() 来测量某些操作需要多长时间,它使用更高分辨率的计时器(取决于底层平台的可用性)。根据平台的不同,您可能会有轻微的错误率。

此外,由于您使用 JavaFX 作为 GUI,因此以下内容复制自 this answer

According to JavaFX Architecture it uses the native event queue for capturing and batching all the events. By capturing it is understood that at that moment in time the event was generated (the user clicked the button). Taking batching into account we have already lost the information about the time the event was generated (unless JavaFX internally keeps that information, you can inspect com.sun.javafx packages for lower level details). Every 1/60th of a second there is a scheduled pulse event. During the pulse event all other JavaFX events like MouseEvent, etc. are fired via the normal JavaFX event dispatching mechanism. It is at this point that your application will receive a notification from JavaFX that an event has occurred. So in an ideal world the difference between the time an event was posted and the time it was handled should be < 0.0166(6) seconds.

简而言之,当实际按下 SPACE 键时和 JavaFX 告诉您何时按下该键时是不同的值,其中差异应该 < 0.0166(6) 秒。所以你必须考虑到这一点。还值得注意的是,如果由于任何原因 JavaFX 应用程序线程被延迟,差异也会增加,从而导致错误率更高。

关于java - 计算按下按钮到执行一行代码之间的延迟的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com