We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Slow performance
Page Index Toggle Pages: 1
Slow performance (Read 394 times)
Slow performance
Jul 7th, 2008, 2:41pm
 
Hi, i've just started programming in Processing. I tried to make my own sort of ambilight with an LED-controller called Chromoflex.

Well everything works the only problem is that it seems to have a delay of about half a second and it doesnt matter if I increase or decrease the resolution the framerate always seems to be around 4-5 fps.

Can anyone help speed up my program?

Keep in mind that I'm not a 'real' programmer.
Here's the code:

-------------------------------------------------------


import processing.serial.*;

Serial myPort;
int[] reset = {202,0,0,0,0,0,254,140,240};
int[] kleur = {202,0,0,0,0,5,126,0,255,0,0,100};
int[] userprog = {202,0,0,0,0,2,126,18,1,149,136};

int hoogte = 60;
int breedte = 80;
int a=0;
int tussen = 0;
int crc = 65535;
int test = 65535;
int crc16 = 0;
int crc16_1 = 0;
int crc16_2 = 0;

float rood_aanpassing = 1;
float groen_aanpassing = 0.7;
float blauw_aanpassing = 0.2;

PImage screenshot;
int rood_gem = 0;
int groen_gem = 0;
int blauw_gem = 0;
int eenmaal = 0;

void reset_chromo(){
 for(int i=0;i<reset.length;i++){
   myPort.write(reset[i]);
 }
 delay(1000);
 overnemen();
}

void overnemen(){
 for(int i=0;i<userprog.length;i++){
   myPort.write(userprog[i]);
 }
}

void dec_to_hex(){
 
 int hexarray[] = {0,0,0,0};
 int b = 3;
 
 while(crc>16){
   int c = crc%16;
   int d = crc - c;
   hexarray[b] = c;
   crc = d / 16;
   b--;
 }
 hexarray[b] = crc;
 crc16_1 = hexarray[0]*16 + hexarray[1];
 crc16_2 = hexarray[2]*16 + hexarray[3];
   
 for( int q=0;q<kleur.length;q++){
   myPort.write(kleur[q]);
 }
 myPort.write(crc16_1);
 myPort.write(crc16_2);
}

void bereken_crc16(){
 crc = 65535;
 for(int i=0;i<kleur.length;i++){
   
   tussen = kleur[i]^crc;
   crc = tussen;
   for(int z=0;z<8;z++){
     
     if((crc&1)==1){
       
       int a = crc >> 1;
       crc = a^40961;
     
   } else {
       tussen = crc;
       crc = tussen >> 1;
     }
   }
 }
}

void setup() {
 frameRate(50);
 size(breedte,hoogte);
 myPort = new Serial(this,"COM9",9600);
 reset_chromo();
}

void screencapture(){
 try {
  Robot robot = new Robot();
  screenshot = new PImage(robot.createScreenCapture(new Rectangle(0,0,screen.width,screen.height)));

} catch (AWTException e) {
}
}
 

void draw() {
screencapture();
image(screenshot, 0, 0, width, height);
loadPixels();
for(int b=0;b<(hoogte+1);b++){
  for(int c=0;c<(breedte+1);c++){
    color currColor = pixels[c+(hoogte*b)];
    rood_gem+=red(currColor);
    groen_gem+=green(currColor);
    blauw_gem+=blue(currColor);
  }
}


rood_gem/=(hoogte*breedte);
groen_gem/=(hoogte*breedte);
blauw_gem/=(hoogte*breedte);

rood_gem*=rood_aanpassing;
groen_gem*=groen_aanpassing;
blauw_gem*=blauw_aanpassing;

kleur[7] = 4;
kleur[8] = rood_gem;
kleur[9] = groen_gem;
kleur[10] = blauw_gem;

overnemen();
bereken_crc16();
dec_to_hex();

kleur[7] = 0;

bereken_crc16();
dec_to_hex();

rood_gem = 0;
groen_gem = 0;
blauw_gem = 0;
println(frameRate);
}

Re: Slow performance
Reply #1 - Jul 7th, 2008, 5:38pm
 
I'm not sure that it will speed up your sketch a lot, but in draw(), instead of

Code:
loadPixels();

for(int b=0;b<(hoogte+1);b++){
for(int c=0;c<(breedte+1);c++){
color currColor = pixels[c+(hoogte*b)];
rood_gem+=red(currColor);
groen_gem+=green(currColor);
blauw_gem+=blue(currColor);
}
}

rood_gem/=(hoogte*breedte);
groen_gem/=(hoogte*breedte);
blauw_gem/=(hoogte*breedte);

rood_gem*=rood_aanpassing;
groen_gem*=groen_aanpassing;
blauw_gem*=blauw_aanpassing;


you can try :

Code:
loadPixels();
for(int i = 0; i <= breedte*hoogte; i++) {
color currColor = pixels[i];
rood_gem += currColor >> 16 & 0xFF;
groen_gem += currColor >> 8 & 0xFF;
blauw_gem += currColor & 0xFF;
}
rood_gem *= rood_aanpassing/(hoogte*breedte);
groen_gem *= groen_aanpassing/(hoogte*breedte);
blauw_gem *= blauw_aanpassing/(hoogte*breedte);


which should give the same result on screen.
Page Index Toggle Pages: 1