Java 检测执行if和else语句的时间

Java 检测执行if和else语句的时间,java,if-statement,time,processing,Java,If Statement,Time,Processing,在processing(基于java)中,只要程序在运行,draw方法就会不断执行。我试图测量if语句中的条件为真的持续时间。 我有一个if声明: if (matrix [3][5]== 3) { System.out.println("Closed"); } else { System.out.println("Opened"); } if (matrix [3][5]== 3) { System.out.println("Closed");

在processing(基于java)中,只要程序在运行,
draw
方法就会不断执行。我试图测量if语句中的条件为真的持续时间。 我有一个if声明:

if (matrix [3][5]== 3) {
      System.out.println("Closed");
}
else {
 System.out.println("Opened");
    } 
if (matrix [3][5]== 3) {
          System.out.println("Closed");
         long estimatedTime = System.nanoTime() - startTime;
         System.out.println("Was opened for"+ estimatedTime);
    }
矩阵[3][5]
的值是动态变化的(我使用Reactivision,根据一些标记的位置,该值将发生变化)。当我运行程序时,条件为false,因此我将:

opened
opened
...
opened
然后条件将为真,因此它将打印:

closed
closed 
etc
最后才转回去开。 我想测量条件为真并打印关闭的时间,以及它何时改变,它保持打开状态的时间等:对于每一段时间,它返回打开或关闭状态,我想知道有多长时间

我在设置中启动了计时器:

void setup () {
startTime = System.nanoTime();
}
我可以在if声明中结束:

if (matrix [3][5]== 3) {
      System.out.println("Closed");
}
else {
 System.out.println("Opened");
    } 
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();
}

你在正确的轨道上。您可以使用
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();
}

你在正确的轨道上。您可以使用
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();
}

你在正确的轨道上。您可以使用
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分支中设置新的开始时间。您应该设置一个额外的标志指示最后一个状态

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分支中设置新的开始时间。您应该设置一个额外的标志指示最后一个状态

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分支中设置新的开始时间。您应该设置一个额外的标志指示最后一个状态

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");
    }
德国劳埃德船级社

尝试在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");
    }
德国劳埃德船级社

尝试在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");
    }
德国劳埃德船级社

尝试在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");
    }

德国劳埃德船级社

您可以在类中保留两个字段,其中您要测量打开状态的时间,如下所示:

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));
}

您可以在类中保留两个字段,其中您要测量打开状态的时间,如下所示:

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));
}

您可以在类中保留两个字段,其中您要测量打开状态的时间,如下所示:

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));
}

您可以在类中保留两个字段,其中您要测量打开状态的时间,如下所示:

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));
}

我认为最好的办法是跟踪当前的“状态”(打开/关闭),并在“状态”更改时重置启动时间。我还没有尝试过下面的方法,但希望它能让你朝着正确的方向出发

首先,您必须定义一个“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");
} 

我认为你最好的办法是跟踪当前的“状态”(打开/关闭),并在“状态”发生变化时重置你的开始时间。我没有尝试以下方法,但希望它能让你朝着正确的方向开始

首先,您必须定义一个“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");
} 

我认为你最好的办法是跟踪当前的“状态”(打开/关闭),并在“状态”发生变化时重置你的开始时间。我没有尝试以下方法,但希望它能让你朝着正确的方向开始

首先,您必须定义一个“lastState”字符串变量(proba