<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #optimize - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23optimize</link>
      <pubDate>Sun, 08 Aug 2021 20:21:26 +0000</pubDate>
         <description>Tagged with #optimize - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23optimize/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Better way to make gravity work</title>
      <link>https://forum.processing.org/two/discussion/27474/better-way-to-make-gravity-work</link>
      <pubDate>Thu, 29 Mar 2018 11:22:37 +0000</pubDate>
      <dc:creator>2000mater</dc:creator>
      <guid isPermaLink="false">27474@/two/discussions</guid>
      <description><![CDATA[<p>Currently working on some planetary stuff, but I don't like the formula I figured out for gravity. I don't like that I had to make different formulas for the right side of the atan and the left side. Also, when my object goes trough the center of the gravity it makes a glichy speed change (click mouse to see). (Please check line13-18 and see if you can help.)
So, what's actually happening guys? And if you know better ways of calculating these stuff please share them with me. Thanks ^,^</p>

<pre><code>float speedx=-0.5,speedy=-0.1,x,y=-70,g;
void setup() {
fullScreen();
rectMode(CENTER);
}
void draw() {
  background(200);
  translate(width/2,height/2);
  ellipse(0,0,50,50);
  rect(x,y,20,20);
  if (x&lt;0) {g=1/sq(dist(0,0,x,y));}
  if (x&gt;=0) {g=-1/sq(dist(0,0,x,y));}
  speedx=speedx+30*g*cos(atan(y/x));
  speedy=speedy+30*g*sin(atan(y/x));
   x=x+speedx;y=y+speedy;
  if (mousePressed) {y=50;x=0;speedx=0;speedy=0;}
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Nested Loop in moving object collision extremely slow</title>
      <link>https://forum.processing.org/two/discussion/26838/nested-loop-in-moving-object-collision-extremely-slow</link>
      <pubDate>Wed, 14 Mar 2018 14:09:18 +0000</pubDate>
      <dc:creator>kaskus</dc:creator>
      <guid isPermaLink="false">26838@/two/discussions</guid>
      <description><![CDATA[<p>Hi, I am trying to create simple collision detection between moving objects from different direction. when the object meet the other object in close range they will detect collision and move up/down try not to collide each other. the problem is when i use nested loop to check the collision after 20 seconds of simulation the object move very slow. is it normal or is there a solution to optimize this. FYI my laptop specification is core i5. Here is my simple code.. thanks</p>

<pre><code>ArrayList&lt;Car&gt; car;
int timer = 1000;
int _timer = 100;

void setup(){
  size(800,800);
  car = new ArrayList();
}

void draw(){
  background(0);

  text("millis="+millis(), 20, 20);

  for (int i = 0; i &lt; car.size(); i++)
  {

    car.get(i).drawCar();
    car.get(i).move(car.get(i).getDx(), car.get(i).getDy());

    for (int j = i+1; j &lt; car.size(); j++){  
      car.get(i).collideCircle(car.get(j));
    }


  }

  if(itstime()){
    car.add(new Car(-20, 360, 50, 30, random_color(255), 1, 2, 0, 1)); //x, y, w, h, c, d, dx, dy, t
    int ran = int(random(1000,1500));
    timer = timer + ran;
  }

  if(_itstime()){
    car.add(new Car(820, 360, 50, 30, random_color(255), -1, -2, 0, 1));
    int ran = int(random(1000,1500));
    _timer = _timer + ran;
  }

}

boolean itstime(){
  return millis() &gt;= timer;
}

boolean _itstime(){
  return millis() &gt;= _timer;
}

color random_color(float v) {
  return color(random(v), random(v), random(v));
}

//CAR CLASS
class Car {
  int x, y; //x position, y position
  int w, h; //weight, height/lenght
  color c; //color
  int direction; // 1 = left-to-right, 2 = right-to-left
  int dx, dy; //dx = x velocity, dy = y velocity
  int turn; //turn=&gt; 1 = Straight, 2 = right, 3 = left
  int radius;

  Car(int x, int y, int w, int h, color c,int direction, int dx, int dy, int turn) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.c = c;;
    this.direction = direction;
    this.dx = dx;
    this.dy = dy;
    this.turn = turn;
    this.radius = 20;
  }

  void drawCar() {
    fill(c);
    ellipse(x, y, radius, radius); 
  }

  int getDx(){
    return dx;
  }

  void setDx(int vx){
    this.dx = dx + vx;
  }

  int getDy(){
    return dy;
  }

  void setDy(int vy){
    this.dy = dy + vy;
  }

  void setDx_zero(){
    this.dx = 0;
  }

  int getDir(){
    return direction;
  }

  void move() {
    this.x = this.x + this.dx;
    this.y = this.y + this.dy;  
  }

  //with parameter
  void move(int dx, int dy) {
    this.dx = dx;
    this.dy = dy;

    this.x = this.x + this.dx;
    this.y = this.y + this.dy;
  }

  void collideCircle(Car other){

    println(direction+"  "+other.getDir()); 
    if(direction != other.getDir()){
      if (radius + other.radius &gt; dist(x, y, other.x, other.y)){
        println("r1="+radius+" r2="+other.radius+ " x1="+x+ " x2="+other.x);
        setDx_zero();
        if(getDy() &lt; 4){
          setDy(1);
        }else{
          setDy(0);
        }

        other.setDx_zero();
        if(other.getDy() &lt; -4){
          other.setDy(0);
        }else{
          other.setDy(-1);
        }
      }else{
        println("No collision");
      }
    }
  } //collideCircle
}  
</code></pre>
]]></description>
   </item>
   <item>
      <title>Performance optimization for p5.js of particles system?</title>
      <link>https://forum.processing.org/two/discussion/25254/performance-optimization-for-p5-js-of-particles-system</link>
      <pubDate>Tue, 28 Nov 2017 16:35:11 +0000</pubDate>
      <dc:creator>qniff</dc:creator>
      <guid isPermaLink="false">25254@/two/discussions</guid>
      <description><![CDATA[<p>The idea of this program is to implement data visualization in form of "Tron" trails (like in the movie), with additional random color change functionality. Eventually it will visualize the way devices move in the building. I used particle system to implement the trail. The problem is that the sketch doesn't run at a constant FPS. I know that functions <strong>randomMovement()</strong> and <strong>changeColor()</strong> look a bit loaded, but profiling indicated that the <strong>addTrail()</strong> function takes the most of the time and provides the greatest load on the program. Right now the program uses some randomly generated coordinates to align and move devices, but eventually the real data will be involved and number of devices will increase up to 1000 and at the moment program is overwhelmed with only 100 of them. So I was wondering if there is a way to optimize this trail system. I am relatively new to Js and am hoping that someone could run through my code to see if it could be optimized. Any help will be greatly appreciated.</p>

<pre><code>let x1 = [];
let y1 = [];
let Devices = [];

function setup() {

    createCanvas(displayWidth, displayHeight);
    getDummyDevices(100);
}

function draw() {

    background(100);
    for(let i = Devices.length - 1; i &gt; 0; i--){
        Devices[i].goTo(x1[i], y1[i]);
        Devices[i].show();
    }

    showFps();
}

function showFps(){
    textSize(32);
    fill(70,250,5);
    text("FPS: " + frameRate().toFixed(2), 10, height - 10);
}

function getDummyDevices(number){

    for(let i = 0; i &lt;= number; i++){

        let x = random(0, width);
        let y = random(0, height);

        let temp = new Device(x,y);
        Devices.push(temp);

        let x1p = random(0, width);
        let y1p = random(0, height);
        x1.push(x1p);
        y1.push(y1p);
    }

}


class Device {

    constructor(x0,y0) {
        this.x = x0;
        this.y = y0;

        this.diameter = 25;                           // diameter of the device
        this.speed = 2;                               // movement speed of the device

        if(random(-1,1) &gt;= 0) {
            this.trailColor = color(255,0,0);         // trailColor: color variable
            this.trailColorString = "red";            // trailColorString: string indicator of color
        }
        else {
            this.trailColor = color(0,0,255); 
            this.trailColorString = "blue";
        }
        this.changeColorPeriod = random(100,200);     // How often device should change color
        this.lastChangedColorTime = 0;                // keeps track of last change of color
        this.colorChangeSpeed = 20;                   // speed of color change of the device (lower -&gt; slower)

        this.deviceColor = color(8, 20, 33);          // color of the device

        this.particles = [];                          // stores particles representing trail of the device
        this.particleFrequency = 7;                   // per how many frames new particle appears
        this.particleCounter = 0;

        this.changeDirectionPeriod = random(50, 150); // How often device should change direction
        this.lastChangedDirectionTime = 0;            // keeps track of last change of direction
        this.currentDirection = random(-1, 1);        // signs direction of device
    }

    show() {
        fill(this.deviceColor);
        ellipse(this.x, this.y, this.diameter);
    }

    goTo(x1, y1) {

        this.randomMovement(x1, y1);
        this.addTrail();
        this.changeColor();
    }

    // simple 'random' movement to destination point, random means device randomly follows x or y first
    randomMovement(x1, y1){

        this.lastChangedDirectionTime += 1;

        if(this.lastChangedDirectionTime &gt;= this.changeDirectionPeriod)
        {
            this.currentDirection *= -1;
            this.lastChangedDirectionTime = 0;
        }

        if(this.currentDirection &lt; 0){

            // simple movement (first x, then y)
            if(!this.inDistanceOf("x", x1)){
                if(this.x &lt; x1) this.x += this.speed;
                else if(this.x &gt; x1) this.x -= this.speed;
            }
            else if(!this.inDistanceOf("y", y1)){
                if(this.y &lt; y1) this.y += this.speed;
                else if(this.y &gt; y1) this.y -= this.speed;
            }
        } else {

            // first y, then x
            if(!this.inDistanceOf("y", y1)){
                if(this.y &lt; y1) this.y += this.speed;
                else if(this.y &gt; y1) this.y -= this.speed;
            }
            else if(!this.inDistanceOf("x", x1)){
                if(this.x &lt; x1) this.x += this.speed;
                else if(this.x &gt; x1) this.x -= this.speed;
            }
        }
    }

    addTrail(){

        if(this.particleCounter &gt;= this.particleFrequency){

            let p = new Particle(this.x, this.y, this.speed, this.trailColor);
            this.particles.push(p);
            this.particleCounter = 0;
        }
        this.particleCounter += 1;

        for(let i = this.particles.length - 1; i &gt;= 0; i--){

            this.particles[i].show();

            if(this.particles[i].finished()){
                this.particles.splice(i, 1);
            }
        }
    }

    // function to periodically change color of the trail
    changeColor(){

        this.lastChangedColorTime += 1;

        if(this.lastChangedColorTime &gt;= this.changeColorPeriod)
        {
            if(this.trailColorString == "red") this.trailColorString = "blue";
            else this.trailColorString = "red";
            this.lastChangedColorTime = 0;
        }

        let r = red(this.trailColor);
        let b = blue(this.trailColor);

        if(this.trailColorString == "blue")
        {
            if(r &gt; 0){
                r -= this.colorChangeSpeed;
                if(r &lt; 122) b += this.colorChangeSpeed;
            }
            else if(r &gt;= 0 &amp;&amp; b &lt; 255)
            {
                b += this.colorChangeSpeed;
            }
        }
        else {
            if(b &gt; 0){
                b -= this.colorChangeSpeed;
                if(b &lt; 122) r += this.colorChangeSpeed;
            }
            else if(b &gt;= 0 &amp;&amp; r &lt; 255)
            {
                r += this.colorChangeSpeed;
            }
        }

        this.trailColor = color(r, 0, b);
    }

    // due to the fact that destination point is not an integer, 
    // I use this function to determine if device is close enough to the destination and can stop moving
    inDistanceOf(axis, xy){
        let distance = 3;
        let toReturn = false;

        let check0, check1;

        if(axis == "x"){
            check0 = this.x - xy;
            check1 = xy - this.xy;

            if((check0 &lt; distance) &amp;&amp; ( check0 &gt; -distance)) toReturn = true;

            if((check1 &lt; distance) &amp;&amp; ( check1 &gt; -distance)) toReturn = true;
        }

        if(axis == "y"){
            check0 = this.y - xy;
            check1 = xy - this.y;

            if((check0 &lt; distance) &amp;&amp; ( check0 &gt; -distance)) toReturn = true;

            if((check1 &lt; distance) &amp;&amp; ( check1 &gt; -distance)) toReturn = true;
        }
        return toReturn;
    }

}


class Particle {

    constructor(x0, y0, speed, color) {
        this.x = x0;
        this.y = y0;
        this.diameter = 20;
        this.alpha = 255;
        this.speed = speed;

        this.r = red(color);
        this.b = blue(color);
    }

    show() {

        noStroke();
        fill(this.r, 0, this.b, this.alpha);
        ellipse(this.x, this.y, this.diameter);

        this.update();
    }

    // steadily decreases transparency of particles to make view of moving trail
    update() {
        if(this.alpha &gt; 200){
            this.alpha -= 1.2*this.speed;
        }
        else if(this.alpha &gt; 150){
            this.alpha -= 1*this.speed;
        }
        else if(this.alpha &gt; 100){
            this.alpha -= 0.8*this.speed;
        }
        else if(this.alpha &gt; 50){
            this.alpha -= 0.5*this.speed;
        }
        else{
            this.alpha -= 0.3*this.speed;
        }
    }

    finished() {
        return this.alpha &lt;= 0;
    }

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Animating with large datasets</title>
      <link>https://forum.processing.org/two/discussion/25225/animating-with-large-datasets</link>
      <pubDate>Mon, 27 Nov 2017 10:51:19 +0000</pubDate>
      <dc:creator>CPG</dc:creator>
      <guid isPermaLink="false">25225@/two/discussions</guid>
      <description><![CDATA[<p>Hi Everyone!</p>

<p>I have been working with P5 for a while now, but in my current project I'm working with large data sets for the first time.
The FPS I'm getting when animating over 2000+ data points is not good, is there a way to improve upon this?</p>

<p>Thanks!</p>
]]></description>
   </item>
   <item>
      <title>How to make rendering on screen faster?</title>
      <link>https://forum.processing.org/two/discussion/25020/how-to-make-rendering-on-screen-faster</link>
      <pubDate>Wed, 15 Nov 2017 08:40:48 +0000</pubDate>
      <dc:creator>mproc</dc:creator>
      <guid isPermaLink="false">25020@/two/discussions</guid>
      <description><![CDATA[<p>I am writing this program to plot graphs on screen. However, the rendering is too slow. Any clues how it can be made faster? Here is the code below. It plots a simple Archimedes spiral.</p>

<pre><code>float r=0,a=0,t=0,x0=0,y0=0,x=0,y=0;
int c=80;

void setup(){
  size(1024,768);
  background(0);
  strokeWeight(1);
  stroke(c,100,150);
}

void draw(){

  translate(width/2,height/2);

   if(t&lt;=30*PI)
    {
      x0=r*cos(a);
      y0=r*sin(a);

      a=t;//360*(sin(t/20)+0.5*sin(t/40));
      r=a;//200*(sin(t/40)+0.5*sin(t/80));

      x=r*cos(a);
      y=r*sin(a);

      line(x0,-y0,x,-y);

      t+=0.1;
   }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Help to optimise proximity particles</title>
      <link>https://forum.processing.org/two/discussion/25012/help-to-optimise-proximity-particles</link>
      <pubDate>Tue, 14 Nov 2017 22:03:19 +0000</pubDate>
      <dc:creator>jmean</dc:creator>
      <guid isPermaLink="false">25012@/two/discussions</guid>
      <description><![CDATA[<p>Hi,
I'm trying to draw lines between particles if they are close enough to each other. 
My code seems to do that however the frame rate drops to pretty much 0.</p>

<p>How can I make this faster? 
Code here: <a rel="nofollow" href="https://codepen.io/jmean/pen/pdwXgO">https://codepen.io/jmean/pen/pdwXgO</a></p>

<p>Thanks</p>
]]></description>
   </item>
   <item>
      <title>Any way to optimize this pixel overlay?</title>
      <link>https://forum.processing.org/two/discussion/23603/any-way-to-optimize-this-pixel-overlay</link>
      <pubDate>Thu, 27 Jul 2017 20:11:21 +0000</pubDate>
      <dc:creator>Abotics99</dc:creator>
      <guid isPermaLink="false">23603@/two/discussions</guid>
      <description><![CDATA[<p>Hi there, I'm currently working on a "retro" looking game and I've made a function that draws a pixelized overlay across the canvas. I'm currently working on optimizing it and would love any help.  It already runs fairly well (60fps) but compared to when it's not running (500+fps) I think it could do better.</p>

<pre><code>void setup() {
  size(1024,896,P2D);
  frameRate(500);
  noStroke();
  smooth(0);
}

void draw() {
  background(0);
  ellipse(mouseX,mouseY,100,100);
  if(mousePressed){
    drawPixels(256,224);
  }
  println(frameRate);
}

void drawPixels(int retroResX, int retroResY) {
  int pixelScale = width/retroResX;
  loadPixels();
  for(int y=0;y&lt;retroResY;y++){
    for(int x=0;x&lt;retroResX;x++){
      for(int py=0;py&lt;pixelScale;py++){
        for(int px=0;px&lt;pixelScale;px++) {
          pixels[((py+(y*pixelScale))*width)+(px+(x*pixelScale))] = pixels[(y*width*pixelScale)+(x*pixelScale)];
        }
      }
    }
  }
  updatePixels();
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Efficiency: How to use fewer loops</title>
      <link>https://forum.processing.org/two/discussion/23397/efficiency-how-to-use-fewer-loops</link>
      <pubDate>Mon, 10 Jul 2017 23:39:40 +0000</pubDate>
      <dc:creator>SnailPropulsionLabs</dc:creator>
      <guid isPermaLink="false">23397@/two/discussions</guid>
      <description><![CDATA[<p>Hi all,
I'm working on a program that uses incoming tweets to draw sections of various svgs.<br />
It works without error, I just don't like how many loops I've ended up using and was wondering how it can be improved.</p>

<p>The top loops are drawing the whole shape and the enhanced loops are drawing the shape sections.
The reason for separating them into individual loops was that having them in one loop meant that things were not drawing when the tweet came in and instead everything was drawn simultaneously.</p>

<p>If there is a way to improve this, i'm all ears.</p>

<p>I haven't included the entire program due to size and that this is the only part i'm looking to optimize.</p>

<p>Thankyou.</p>

<pre><code>    if (state == S_PLAY) { 
    shapeMode(CORNER);
    noStroke();
    fill(shapeColor);
    for (int i = 0; i &lt; numLollipops; i++) {
      lollipopFull.disableStyle();
      shape(lollipopFull, i*SIZE, SIZE, SIZE, SIZE);
      numLollipops = check(i*SIZE, numLollipops);
    }
    for (int i = 0; i &lt; numCircles; i++) {
      circleFull.disableStyle();
      shape(circleFull, (width-SIZE)-i*SIZE, SIZE*3, SIZE, SIZE);
      numCircles = check(i*SIZE, numCircles);
    }
    for (int i = 0; i &lt; numLines; i++) {
      lineFull.disableStyle();
      shape(lineFull, i*SIZE, SIZE*5, SIZE, SIZE);
      numLines = check(i*SIZE, numLines);
    }
    for (int i = 0; i &lt; numSeptagons; i++) {
      septagonFull.disableStyle();
      shape(septagonFull, (width-SIZE)-i*SIZE, SIZE*7, SIZE, SIZE);
      numSeptagons = check(i*SIZE, numSeptagons);
    }
    for (int i = 0; i &lt; numSq; i++) {
      squareFull.disableStyle();
      shape(squareFull, i*SIZE, SIZE*9, SIZE, SIZE);
      numSq = check(i*SIZE, numSq);
    }
    for (int i = 0; i &lt; numTriStars; i++) {
      starFull.disableStyle();
      shape(starFull, (width-SIZE)-i*SIZE, SIZE*11, SIZE, SIZE);
      numTriStars = check(i*SIZE, numTriStars);
    }
    for (int i = 0; i &lt; numSeptagrams; i++) {
      septagramFull.disableStyle();
      shape(septagramFull, i*SIZE, SIZE*13, SIZE, SIZE);
      numSeptagrams = check(i*SIZE, numSeptagrams);
    }
    shapeMode(CENTER);
    for (Lollipop l : lollipops) l. draw();
    for (Septagon s : septagons) s. draw();
    for (Line li : lines) li.draw();
    for (Circle c : circles) c. draw();
    for (Square sq : squares) sq.draw();
    for (TriStar t : triStars) t. draw();
    for (Septagram sg : septagrams) sg.draw();
    words.clear();
</code></pre>
]]></description>
   </item>
   <item>
      <title>how to improve performance for particle systems</title>
      <link>https://forum.processing.org/two/discussion/18565/how-to-improve-performance-for-particle-systems</link>
      <pubDate>Sat, 15 Oct 2016 19:43:04 +0000</pubDate>
      <dc:creator>flowen</dc:creator>
      <guid isPermaLink="false">18565@/two/discussions</guid>
      <description><![CDATA[<p>dear creatives,</p>

<p>I'm building a particle system, based on Daniel Shiffman's code from nature of code. Drawing triangle vertices when distance is within the defined range. Quite fast I get massive fps drops, which i sort of expected as it's 3 loops within each other.</p>

<p>I've been briefly told about binned particle system, for which I could only find some old thread containing dead links. <a href="https://forum.libcinder.org/topic/kyle-mcdonald-s-binned-particle-system" target="_blank" rel="nofollow">https://forum.libcinder.org/topic/kyle-mcdonald-s-binned-particle-system</a></p>

<p>I wonder if there are other known techniques like this and if you know some resources or perhaps even a tutorial ;)</p>

<p>Thx in advance</p>
]]></description>
   </item>
   <item>
      <title>Low frame rate on Android</title>
      <link>https://forum.processing.org/two/discussion/15759/low-frame-rate-on-android</link>
      <pubDate>Wed, 30 Mar 2016 05:54:02 +0000</pubDate>
      <dc:creator>phoebus</dc:creator>
      <guid isPermaLink="false">15759@/two/discussions</guid>
      <description><![CDATA[<p>Hello</p>

<p>I run a sketch that makes 180 ellipses rotating around a point.
each of those ellipses are instances of a Satellite Class that render the ellipse using pushMatrix(), translate(...), rotate(...) and ellipse(...)</p>

<p>Running this sketch on my pc , the FPS is stuck at 60, but running it on my Nvidia Shield K1, I get around 30 FPS</p>

<p>-1- is it that demanding to run 180 objects using primitive shapes ?</p>

<p>I tried with a little more complicated shapes, using 4 primitives, it gets to 22 FPS</p>

<p>I tried using PShape to build Shapes of 4 primitives and it gets to 18 FPS</p>

<p>-2- Am I facing a weakness in Processing for android ?</p>

<p>-3- Is there a way to solve that ?</p>

<p>(I tried using P2D and it crashes with
"A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x9e5ad000 in tid 17144 (GLThread 46915)" message)</p>

<p>cheers</p>

<p>update: using a 50x50 PGraphics instead of PShape gets me 11 FPS</p>

<p>update 2: I finally got it to run on P2D (I don't know why, I just copied my whole code, classes and all, to a blank app that was running P2D just fine) and here is the result:</p>

<p>with my  180 satellites being mere ellipses my FPS is 25 :(
(with them being points(), I'm back over 60 FPS)</p>

<p>is anybody here ?</p>
]]></description>
   </item>
   <item>
      <title>Need an optimization for this code...</title>
      <link>https://forum.processing.org/two/discussion/5831/need-an-optimization-for-this-code</link>
      <pubDate>Mon, 16 Jun 2014 17:17:47 +0000</pubDate>
      <dc:creator>aewsb</dc:creator>
      <guid isPermaLink="false">5831@/two/discussions</guid>
      <description><![CDATA[<p>Hello everyone,</p>

<p>I would appreciate if someone could help me to optimize this code. This code takes reads from arduino code which is an array separated by , and then splits and use each number to color a rectangle. The screen has 32 rectangles. This code runs too slow on android phones\tablets and I don't know the problem. I've tried HTC One/Samsung S4/Tegra Note 7.</p>

<p>Here is the code:</p>

<pre><code>    import android.content.Intent;
    import android.os.Bundle;
    import ketai.net.bluetooth.*;
    import ketai.ui.*;
    import ketai.net.*;

    PFont fontMy;
    boolean bReleased = true; //no permament sending when finger is tap
    KetaiBluetooth bt;
    boolean isConfiguring = true;
    String info = "";
    float num[] = new float[32];
    int i = 0;
    int x = 0;
    int y = 0;
    int Height = 0;
    int Width = 0;
    KetaiList klist;
    ArrayList devicesDiscovered = new ArrayList();

    //********************************************************************
    // The following code is required to enable bluetooth at startup.
    //********************************************************************

    void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      bt = new KetaiBluetooth(this);
    }

    void onActivityResult(int requestCode, int resultCode, Intent data) {
      bt.onActivityResult(requestCode, resultCode, data);
    }

    void setup() {
      size(displayWidth, displayHeight, JAVA2D);
      frameRate(30);
      orientation(LANDSCAPE);
      background(0);

      //start listening for BT connections
      bt.start();
      //at app start select device…
      isConfiguring = true;
      //font size
      fontMy = createFont("SansSerif", 40);
      textFont(fontMy);
      Height = (displayHeight - (displayHeight % 4));
      Width = (displayWidth - (displayWidth % 8));
    }

    void draw() {
      //at app start select device
      if (isConfiguring)
      {
        ArrayList names;
        background(0, 0, 0);
        klist = new KetaiList(this, bt.getPairedDeviceNames());
        isConfiguring = false;
      }
      else
      {
        //    background(0, 50, 0);
        //  if((mousePressed) &amp;&amp; (bReleased == true))
        //  {
        // //send with BT
        //  byte[] data = {'s','w','i','t','c','h','\r'};
        //  bt.broadcast(data);
        // //first tap off to send next message
        //  bReleased = false;
        //  }
        //  if(mousePressed == false)
        //  {
        //  bReleased = true; //finger is up
        //  }
        //print received data
        //    fill(255);
        //      textAlign(LEFT);
        //     text(info.length(), 20, 104);
        //       text(w,20,104);
        //       text(h,20,130);
        num = float(split(info, ','));
        if (num.length == 32) {
          //      info = "";
          fill(255);
          noStroke();
          i = 0;
          for (int y=0; y&lt;Height; y+=Height/4) {
            for (int x=0; x&lt;Width; x+=Width/8) {
              fill(num[i]);
              rect(x, y, Width/8, Height/4);
              i++;
            }
          }
          num = new float [32];
        }
      }
    }

    void onKetaiListSelection(KetaiList klist) {
      String selection = klist.getSelection();
      bt.connectToDeviceByName(selection);
      //dispose of list for now
      klist = null;
    }

    //Call back method to manage data received
    void onBluetoothDataEvent(String who, byte[] data) {
      if (isConfiguring)
        return;
      //received

      if (info.length() &gt;= 62)
        info = "";

      info += new String(data);
    }

    // Arduino+Bluetooth+Processing 
    // Arduino-Android Bluetooth communication
</code></pre>

<p>Thank you in advance :)</p>
]]></description>
   </item>
   </channel>
</rss>