# Any way to speed up this ASCII art clock on Raspberry Pi

**URL:** <https://discourse.processing.org/t/any-way-to-speed-up-this-ascii-art-clock-on-raspberry-pi/36047>\
**Category:** Processing for Pi\
**Created:** [March 30, 2022, 10:35pm UTC](https://discourse.processing.org/t/any-way-to-speed-up-this-ascii-art-clock-on-raspberry-pi/36047 "2022-03-30T22:35:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [March 30, 2022, 10:35pm UTC](https://discourse.processing.org/t/any-way-to-speed-up-this-ascii-art-clock-on-raspberry-pi/36047/1 "2022-03-30T22:35:53Z")

</div>

Hi

I have this ascii art clock working. The point of the project is to run it on a raspberry pi with a phone sized screen and make it into a bedside clock with a concrete enclosure.

Sadly it runs quite slowly on the pi 4. Can anyone suggest any ways to speed the code up? Or am I just at the limit of processing on a pi?

The pi is running regular 32 bit raspian and I downloaded the processing install from [here](https://pi.processing.org/download/). The premade image for pi doesn’t work on pi 4.

```auto
int sw = 1920; // screen width
int sh = 1080; // screen height
int bw = 165; // number of characters in x (buffer width)
int bh = int(float(bw) * 0.5625); // number of characters in y (buffer height)
float r = 0; // for rotations
PGraphics pg;
String chars = " .o**OX@@"; // the pixels will be mapped to these characters by brightness
float factor;

int ts; // text size of pre render clock

float tw; // text width of pre render clock
float th; // text height of pre render clock
String time;
float rotate;
float rotate2;

float p;
//float q;

Boolean init = true;

void setup() {

  size(1920, 1080, P3D);
  //fullScreen(P3D);

  pg = createGraphics(bw, bh, P3D);
  factor = sw/bw;

  ts = int(float(bw)*0.35);
  textSize(sw/bw);
}

void draw() {

  background(0);
  drawText();

  pg.loadPixels();

  int c = 0;

  // iterate through pixels and map to characters

  for (int i = 0; i < bh; i ++) {
    for (int j = 0; j < bw; j++) {
      p = brightness(pg.pixels[c])/255*9; // map the brightness value to something we can index the string with
      char l = chars.charAt(int(p)); // find the char at that index of the string
      text(l, j*factor, i*factor); // draw that char at the right location
      c++;
    }
  }
}

void drawText() {

  pg.beginDraw();
  
  if (init) {
    pg.textSize(ts);
    tw = pg.textWidth(hour() + ":" + nf(minute(), 2));
    th = pg.textAscent()+pg.textDescent();
  }
  
  pg.background(65);
  pg.translate(bw/2, bh/2);
  time = hour() + ":" + nf(minute(), 2);
  rotate = sin(r*0.01);
  rotate2 = sin((r+0.5)*0.015);
  //pg.rotate(radians(rotate*15));
  pg.rotateY(radians(rotate*35));
  pg.rotateX(radians(rotate2*35));
  pg.text(time, -tw/2, th/4);
  r += 1;
  pg.endDraw();
  pg.filter(BLUR, 1);
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 31, 2022, 12:46am UTC](https://discourse.processing.org/t/any-way-to-speed-up-this-ascii-art-clock-on-raspberry-pi/36047/2 "2022-03-31T00:46:26Z")

</div>

Hello @arcadeperfect ,

I did some tests on my Windows 10 PC.

This is a cool effect but reduces frameRate:  
`pg.filter(BLUR, 1);`

I tried a pseudo blur with:

```auto
pg.textSize(ts/5);
//...
pg.translate(bw/2, bh/2);
pg.scale(5);

```

I added this to see the image to get a sense of blur before processing:  
`image(pg, 0, 0);`

You could set this after you initialize:  
`init = false;`

I replaced your brightness with this to do only integer math:  
`int r = 9*(pg.pixels[c] & 0xFF)/255;`

That’s all I got for now…

`:)`

---

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.processing.org/u/arcadeperfect)\
**Post date:** [March 31, 2022, 3:07am UTC](https://discourse.processing.org/t/any-way-to-speed-up-this-ascii-art-clock-on-raspberry-pi/36047/3 "2022-03-31T03:07:12Z")

</div>

Ha whoops the whole point of that init condition was to set it to false after it ran… somehow I forgot to include that crucial line…

Thanks for the suggestions will test!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 31, 2022, 10:37am UTC](https://discourse.processing.org/t/any-way-to-speed-up-this-ascii-art-clock-on-raspberry-pi/36047/4 "2022-03-31T10:37:30Z")

</div>

Hello @arcadeperfect ,

This is what I use (_or some variation_) to test performance:

```auto
void setup() 
  {
  size(200, 200);
  frameRate(60) // This is default on my PC and my monitor refresh rate
  //frameRate(500); // I set this higher for *testing only* to see what elements of code slow things down. 
  }

void draw() 
  {
  background(0);
  if(frameCount%15 == 0) //Every 15 frames
    {
    surface.setTitle(str(int(frameRate)));
    println(frameRate);
    }
  }

```

`:)`
