[英]Detect for how long if and else statements were executed
在处理(基于Java)中,只要程序正在运行, draw
方法就会不断执行。 我正在尝试测量if语句中的条件为真的时间。 我有一个if语句:
if (matrix [3][5]== 3) {
System.out.println("Closed");
}
else {
System.out.println("Opened");
}
matrix [3][5]
值动态变化(我使用Reactivision,并且根据某些标记的位置,该值将变化)。 当我运行程序时,条件为false,因此我将得到:
opened
opened
...
opened
然后条件将为true,因此它将打印:
closed
closed
etc
最终才转回打开。 我想测量条件为真并关闭打印的时间,以及它何时更改,保持打开状态的时间等:对于每次返回打开或关闭的时间,我想知道有多长时间。
我在设置中启动了一个计时器:
void setup () {
startTime = System.nanoTime();
}
我可以以if语句结尾:
if (matrix [3][5]== 3) {
System.out.println("Closed");
long estimatedTime = System.nanoTime() - startTime;
System.out.println("Was opened for"+ estimatedTime);
}
所以我知道它已经打开多久了。 但是,如何才能使它再次开始以使其现在测量关闭了多长时间,然后又来回打开了呢? 我不知道这个
您走在正确的轨道上。 您可以使用nanoTime()
或millis()
函数来记录开始时间,然后只需使用相同的函数来记录当前时间。 从当前时间中减去开始时间,以获取经过时间。
这是一个示例,跟踪自用户单击以来经过了多少时间:
int start;
void setup(){
size(200, 100);
start = millis();
}
void draw(){
background(0);
int now = millis();
int elapsed = now - start;
text("Elapsed: " + elapsed, 25, 25);
}
void mouseClicked(){
start = millis();
}
您可以在else分支中设置新的开始时间。 并且您应该设置一个额外的标志来指示最后的状态。
if (matrix [3][5]== 3) {
System.out.println("Closed");
if (isOpen)
{
isOpen=false;
System.out.println("Runtime: " + (System.nanoTime() - startTime));
}
}
else {
if (!isOpen)
{
startTime=System.nanoTime();
isOpen = true;
System.out.println("Opened");
}
}
这样的事情。 结果取决于开关频率。 对于不同的调用,“ System.nanoTime()”可能返回相同的时间。
尝试在else中重复您的setup()逻辑:
boolean flag = true;// opened is true.
if (matrix[3][5] == 3) {
if (flag) {
long estimatedTime = System.nanoTime() - startTime;
System.out.println("Was opened for" + estimatedTime);
startTime = System.nanoTime();
flag = false;
}
System.out.println("Closed");// or place in the nested if to be less verbose.
} else {
if (!flag) {
long estimatedTime = System.nanoTime() - startTime;
System.out.println("Was closed for" + estimatedTime);
startTime = System.nanoTime();
flag = true;
}
System.out.println("Opened");
}
GL!
您可以在班级中保留2个字段,要在其中测量打开状态的时间,如下所示:
private Long startOpenedStatus=null;
private Long endOpenedStatus=null;
private int[][] matrix=new int[5][5];
public void yourMethod(){
//...
if (matrix [3][5]== 3) {
stopMeasure();
System.out.println("Closed");
}
else {
startMeasure();
System.out.println("Opened");
}
//...
}
/** take time of start opened status, takes only one (first time method fire) in opened session */
private void startMeasure(){
if(startOpenedStatus==null){
//reset time of end the opened status
endOpenedStatus=null;
startOpenedStatus=new GregorianCalendar().getTimeInMillis();//returns actual time in miliseconds for start of opened status
}
}
/** take time of end opened status, takes only one time in closed session*/
private void stopMeasure(){
if(endOpenedStatus==null){
endOpenedStatus=new GregorianCalendar().getTimeInMillis();//returns actual time in miliseconds for end of opened Status
long timeInMilis=endOpenedStatus-startOpenedStatus; //this is interwal between end and start of time (in miliseconds)
System.out.println("opened status was for "+ timeInMilis+" in miliseconds");
System.out.println("opened status was for "+reaclculateToSeconds(timeInMilis)+" in seconds");
startOpenedStatus=null;
}
}
private BigDecimal reaclculateToSeconds(long timeInMiliseconds){
BigDecimal timeInSeconds=new BigDecimal(timeInMiliseconds).divide(new BigDecimal(1000), MathContext.DECIMAL128);
return timeInSeconds.round(new MathContext(3));
}
我认为您最好的选择是跟踪当前的“状态”(打开/关闭)并在“状态”更改时重置startTime。 我没有尝试过以下方法,但希望它能使您朝正确的方向入手。
首先,您必须定义一个“ lastState”字符串变量(可能在定义“ startTime”的同一位置,并在设置中对其进行初始化。如果您知道初始状态,则将其设置为该值。我仅以“ NotSet”为例) 。
void setup () {
startTime = System.nanoTime();
lastState = "NotSet";
}
然后,您需要一种将当前状态与最后一个状态进行比较并在状态发生变化时输出经过时间的方法。
private static void logElapsed (String currentState) {
if(!lastState.Equals(currentState)) {
// state has changed so output the elapsed time and set the lastState to the currentState
long estimatedTime = System.nanoTime() - startTime;
// reset the startTime to keep track of the elapsed time of the new state
startTime = System.nanoTime();
// Output how long the last state
System.out.println("Was " + lastState + " for "+ estimatedTime);
// reset the lastState to the current state
lastState = currentState;
}
}
最后,您只需要在if / else块中调用它。
if (matrix [3][5]== 3) {
System.out.println("Closed");
logElapsed("Closed");
}
else {
System.out.println("Opened");
logElapsed("Opened");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.