1

I need to determine how long an if statement was executed. I made a simple piece of code to simplify my case:

import org.joda.time.DateTime;

int a;

void setup() {
  int a = 1;

}

void draw() {

  if (a==1) {
    System.out.println(" a is equal to 1");
  }

  else {
    System.out.println(" a is not equal to 1"); 
}

}

In Processing, the draw method keeps on being executed forever. So it will constantly check if a is equal to 1. In my program, a's value is going to change dynamically based on Reactivision: if a particular element is detected, a will be equal to 1. If not, it will be equal to 0.

I want to know how long has the if statement been executed (to know how long the particular element will be detected).

If I use:

void draw() {

  long startTime = System.nanoTime();    
 if (a==1) {
  System.out.println(" a is equal to 1");
 }  
long estimatedTime = System.nanoTime() - startTime;

  else {
    System.out.println(" a is not equal to 1"); 
}

}

each time the draw method will be executed to check if a is equal to 1, it will reset startTime to the current time so it won't be able to add the time already elapsed.

I thought of using joda time, but is there a way to make it "record" how long the if statement was executed ?

Graham Slick
  • 6,692
  • 9
  • 51
  • 87

1 Answers1

1

The standard way to measure elapsed time in Java is use System.nanoTime() as a benchmark.

   long startTime = System.nanoTime();    
     if (a==1) {
      System.out.println(" a is equal to 1");
     }  
    long estimatedTime = System.nanoTime() - startTime;

You should not use System.currentTimeMillis(), see this answer for why.

edit. To see how long a == 1:

import org.joda.time.DateTime;

int a;
long startTime = null;
void setup() {
  int a = 1;
startTime = System.nanoTime();
}

void draw() {

  if (a==1) {
    System.out.println(" a is equal to 1");
  }

  else {
    long estimatedTime = System.nanoTime() - startTime;

    System.out.println(" a is not equal to 1" + "took" + estimatedTime); 
}

}
Community
  • 1
  • 1
discipliuned
  • 916
  • 7
  • 13
  • thanks for the explanation. However, if I place the startTime a the beginning of the draw method, it will be set to 0 each time the method is executed so it won't be able to measure for how long the if statement was executed – Graham Slick Jul 22 '15 at 18:57
  • System.nanoTime() does not return 0. I don't understand what you are trying to accomplish. – discipliuned Jul 22 '15 at 19:00
  • Since the draw method checks constantly for the value of 'a', if I write: void draw () {  long startTime = System.nanoTime(); if (a==1) { System.out.println(" a is equal to 1"); } long estimatedTime = System.nanoTime() - startTime; } each time the draw method checks for a's value, it will reset the value of startTime to 0 so it won't work. If a is not changing for a while, the draw method will keep on executing the if statement. I want to know for how long the if statement was executing, in other words for how long has a been equal to 1 – Graham Slick Jul 22 '15 at 19:04
  • why don't you just add the startTime line to setUp() method and add the estimatedTime line to the "a is not equal to 1" line? – discipliuned Jul 22 '15 at 19:18
  • Take a look at my edit, is that what you're looking for? – discipliuned Jul 22 '15 at 19:21