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 › frameRate low! How to improve
Page Index Toggle Pages: 1
frameRate low! How to improve? (Read 359 times)
frameRate low! How to improve?
Jan 11th, 2009, 9:37am
 
Hi, I have a newbie question: I tried to make a simple slideshow with a transition effect.  I found that background() is fast, but image() and PImage.pixels[] are rather slow; I can't beat 30fps (max 24fps or so).  

Since I've seen numbers like 300fps on the Processing discussions, I suspect I'm doing something wrong.  Could someone give me a hint?  Here's my sample code, which is simple but can't go faster than 48fps with my PC (winxp, AthlonX2 5000+ 2.6GHz, 3GB RAM, GeForce6100):

//import processing.opengl.*;
PImage a, b;
PGraphics pg;
void setup()
{
 size(640, 480, P3D); // P2D similar; OPENGL slower
 a = loadImage("small.jpg");
 a.resize(width, height);
 b = loadImage("medium.jpg");
 b.resize(width, height);
 frameRate(120); // fps

 a.loadPixels();
 b.loadPixels();
 pg = createGraphics(width, height, P3D);
}

void draw()
{
 float ratio;
 ratio = frameCount / (60.0 * 10);
 ratio = constrain(ratio, 0.0, 1.0);
 transition(a, b, ratio);
 if (frameCount % 100 == 0) println(frameRate);
}

void transition(PImage a, PImage b, float ratio)
{
 int method = 0; // select 0, 1, 2, 3

 switch (method) {
 case 0: // 48fps
   background(a);
   tint(255, ratio * 255);
   image(b, 0, 0, width, height);
   noTint();
   break;
 case 1: // 40fps
   int x, y, loc;
   loadPixels();
   for (x = 0; x < width; x++)
     for (y = 0; y < height; y++) {
       loc = x + y * width;
       pixels[loc] = lerpColor(a.pixels[loc], b.pixels[loc], ratio);
     }
   updatePixels();
   break;
 case 2: // 44fps
   pg.beginDraw();
   pg.background(a);
   //pg.background(b);
   pg.tint(255, ratio * 255);
   pg.image(b, 0, 0, width, height);
   pg.noTint();
   pg.endDraw();
   background(pg);
   break;
 case 3: // 45fps
   background(a);
   blend(b, 0, 0, b.width, b.height, 0, 0, width, height, BLEND);
   break;
 }
}

Any suggestion will be appreciated.  Thanks.
Re: frameRate low! How to improve?
Reply #1 - Jan 11th, 2009, 11:19am
 
It might depend on platform: it seems Mac users (not sure for other Unix platforms), they can reach high numbers. On my old system, on a very simple sketch, I can reach 60, I am closer of 30 to 40 on something more complex.
Your sketch is shown at around 30...

Note that anything beyond 25 is theoretically a bit of luxury (that's the classical framerate of normal video) although HD videos goes up to 100Hz (or even 200?). But I don't know if it is noticeable. Even less in effects like your.
Re: frameRate low! How to improve?
Reply #2 - Jan 12th, 2009, 9:47am
 
Thanks for reply.  So I guess my fps is a normal, expected number, right?  

I'm not trying to do ultrafast framing, but when I try to do something a little bit more than simple stuff (e.g., with several img()'s or pixel tweakings in draw()), the fps easily drops down to 11-15.  

I probably need to go for non-realtime way, saving every frame and reconstruct them later on into 25fps video.  But this solution seems headache 'cause I like my video synchronized with music via minim.  

Or: I noticed background() is very fast, so what I need would be background() with tint (or alpha) facility.  I suspect the slowness of image() comes from rescaling, which I don't need in this specific case.  

Cheeeeeers
Page Index Toggle Pages: 1