jeudi 30 juillet 2015

Detect for how long if and else statements were executed

In processing (which is based on java), the draw method is constantly being executed as long as the program is running. I'm trying to measure for how long the condition in the if statement was true. I have a if statement:

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

The value of matrix [3][5]changes dynamically (I use Reactivision, and based on some markers position, this value will change). When I run the program, the condition is false so I'll have:

opened
opened
...
opened

and then the condition will be true so it will print:

closed
closed 
etc

Before eventually turning back to opened. I want to measure for how long the condition was true and printed closed, and when it changes, for how long it stayed opened etc: for each lapse of time it returned opened or closed, I want to know for how long.

I started a timer in my setup:

void setup () {
startTime = System.nanoTime();
}

that I could end in the if statement:

if (matrix [3][5]== 3) {
          System.out.println("Closed");
         long estimatedTime = System.nanoTime() - startTime;
         System.out.println("Was opened for"+ estimatedTime);
    }

So I would know for how long it's been opened. But how can I make it start again to make it measure now for how long it's closed, then opened etc back and forth ? I can't figure this out

Aucun commentaire:

Enregistrer un commentaire