检测 if 和 else 语句执行了多长时间

Detect for how long if and else statements were executed

在处理中(基于java),只要程序运行ning,draw方法就一直在执行。我正在尝试测量 if 语句中的条件为真的时间。 我有一个 if 语句:

if (matrix [3][5]== 3) {
      System.out.println("Closed");
}
else {
 System.out.println("Opened");
    } 

matrix [3][5]的值是动态变化的(我用的是Reactivision,根据一些标记的位置,这个值会变化)。当我 运行 程序时,条件为假,所以我将有:

opened
opened
...
opened

然后条件为真,所以它会打印:

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() 函数来记录开始时间,然后只需使用相同的函数来记录 当前 时间。从当前时间减去开始时间得到你的 elapsed 时间。

下面是一个跟踪用户点击后经过了多长时间的示例:

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()" 可能 return 同一时间不同的调用。

尝试在 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!

您可以在class中保留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));
}

我认为最好的办法是跟踪当前 "state" (open/closed) 并在 "state" 更改时重置 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");
}