# Calculating elapsed microseconds

**URL:** <https://discourse.processing.org/t/calculating-elapsed-microseconds/42646>\
**Category:** Coding Questions\
**Created:** [August 23, 2023, 8:44pm UTC](https://discourse.processing.org/t/calculating-elapsed-microseconds/42646 "2023-08-23T20:44:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 23, 2023, 8:44pm UTC](https://discourse.processing.org/t/calculating-elapsed-microseconds/42646/1 "2023-08-23T20:44:21Z")

</div>

Hi there!

I am measuring the elapsed time of a code in a for-loop in `draw()`:

```auto
void draw(){
  for(byte i = 0; i < 10; i++){
    time_prev = millis();
    
    //code
    
    time_now = millis();
    deltaT = time_now - time_prev;
  }
}

```

The problem is that the elapsed time in a for-loop is smaller than 1 ms, so deltaT is equal to 0 each time… Is there any way to measure the elapsed time in microseconds?

Thanks in advance,

MrImskiy

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 23, 2023, 10:01pm UTC](https://discourse.processing.org/t/calculating-elapsed-microseconds/42646/2 "2023-08-23T22:01:01Z")

</div>

You might try this

```auto
void setup() {
  size(400, 400);
  frameRate(5);
}

void draw() {
  background(128);
  stroke(0);
  strokeWeight(1.5);
  int r = (int)random(160, 255);
  int g = (int)random(160, 255);
  int b = (int)random(160, 255);
  fill(color(r, g, b));

  long time = System.nanoTime();
  ellipse(random(100, 300), random(100, 300), random(50, 300), random(50, 300));
  time = System.nanoTime() - time;

  println(time+ " nanoseconds " + time/1000 + " microseconds");
}

```

There are some things you should be aware of when using `System.nanoTime()` read [here](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#nanoTime--)

---

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 24, 2023, 3:54am UTC](https://discourse.processing.org/t/calculating-elapsed-microseconds/42646/4 "2023-08-24T03:54:12Z")

</div>

Thanks @quark ! 🙂 That seems to do the job!
