# Execution time of one program

**URL:** https://discourse.processing.org/t/execution-time-of-one-program/253
**Category:** Processing
**Created:** [May 22, 2018, 9:13am UTC](https://discourse.processing.org/t/execution-time-of-one-program/253 "2018-05-22T09:13:54Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![totovr76](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/totovr76/32/50_2.png) [@totovr76](https://discourse.processing.org/u/totovr76)
#### Post date: [May 22, 2018, 9:13am UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/1 "2018-05-22T09:13:54Z")

</div>

Hello! How can I measure the **execution time** of one program?

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [May 22, 2018, 12:50pm UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/2 "2018-05-22T12:50:59Z")

</div>

Hi! Not sure what you mean with execution time of one program, as Processing programs often stay running until you close them. This can be used to measure execution time of parts of your program:

```java
  long t = System.currentTimeMillis();
  // do slow stuff
  println(System.currentTimeMillis() - t);

```

With this you can find out for how long the program has run until you press ESC:

```auto
void keyPressed() {
  if(key == ESC) {
    println(millis());
  }
}

```

Or did you have something else in mind?

---

<div class="post-metadata">

### Author: ![totovr76](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/totovr76/32/50_2.png) [@totovr76](https://discourse.processing.org/u/totovr76)
#### Post date: [May 27, 2018, 11:33am UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/3 "2018-05-27T11:33:37Z")

</div>

Sorry! I just saw the reply, as far as I see this measure the global timing of the program, but is it possible to measure the timing since one “flag” is activated until is deactivated?

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [May 27, 2018, 2:11pm UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/4 "2018-05-27T14:11:35Z")

</div>

I think that’s the first example I posted:

```java
  long t = System.currentTimeMillis(); // start "stopwatch"
  // ...
  // do slow stuff 
  // ...
  println(System.currentTimeMillis() - t); // stop "stopwatch"

```

Maybe you didn’t notice the `- t` part?

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 27, 2018, 2:22pm UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/5 "2018-05-27T14:22:34Z")

</div>

> [@totovr76](#):
>
> is it possible to measure the timing since one “flag” is activated until is deactivated?

Yes. Here is an example of a randomly starting and stopping a simple timer and checking the result.

```auto
boolean flag;
int startTime;
int stopTime;

void setup(){
}

void draw(){
  float rand = random(1);
  println(rand);
  if(!flag && rand>0.99){
    flag = true;
    startTime = millis();
  }
  
  if(flag && rand<0.01){
    stopTime = millis();
    int result = stopTime - startTime;
    println("start", startTime, "stop", stopTime, "result", result);
    exit();
  }
}

```

and here is a simple example of setting a countdown duration and having it triggered:

```auto
boolean flag;
int startTime;
int duration;

void setup(){
  duration = 2000;
}

void draw(){
  float rand = random(1);
  println(rand);
  if(!flag && rand>0.99){
    flag = true;
    startTime = millis();
  }
  
  if(millis()-startTime > duration){
    int result = millis();
    println("start", startTime, "duration", duration, "stopTime", result);
    exit();
  }
}

```

…and if you need more timing functions, there are some great libraries full of features on the Libraries resource page:

> **[Libraries](https://processing.org/reference/libraries/)**
>
> Extend Processing beyond graphics and images into audio, video, and communication with other devices.

…in particular, Timing Utilities and Countdown Timer:

> **[GitHub - Lord-of-the-Galaxy/Timing-Utilities: Timing utilities library for...](https://github.com/Lord-of-the-Galaxy/Timing-Utilities)**
>
> Timing utilities library for Processing. Contribute to Lord-of-the-Galaxy/Timing-Utilities development by creating an account on GitHub.

> **[GitHub - dhchoi/processing-countdowntimer: CountdownTimer Library for Processing](https://github.com/dhchoi/processing-countdowntimer)**
>
> CountdownTimer Library for Processing. Contribute to dhchoi/processing-countdowntimer development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 27, 2018, 2:38pm UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/6 "2018-05-27T14:38:45Z")

</div>

> [@jeremydouglass](#):
>
> … and if you need more timing functions, there are some great libraries full of features on…

I’ve got a raw “.java” pseudo-library here too called “Countdown Class”: 😇

- [Countdown Class library for Java, JS & Python - Processing 2.x and 3.x Forum](https://Forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python)

> <https://gist.github.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21>

---

<div class="post-metadata">

### Author: ![totovr76](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/totovr76/32/50_2.png) [@totovr76](https://discourse.processing.org/u/totovr76)
#### Post date: [May 28, 2018, 10:25am UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/7 "2018-05-28T10:25:24Z")

</div>

Thanks to all for yours answers! there were very helpful 😀

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [June 10, 2018, 2:47pm UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/8 "2018-06-10T14:47:15Z")

</div>

Hey, @GoToLoop – I feel like perhaps I have left yours out of a list several times. Have you ever considered making it a library and sharing it on the Libraries page for ease of finding?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 10, 2018, 5:56pm UTC](https://discourse.processing.org/t/execution-time-of-one-program/253/9 "2018-06-10T17:56:56Z")

</div>

You mean as an official Processing library? It’s so small I don’t think it’s worth it. 😔
