[SOLVED] Simple loops drastically slows down sketch

Can someone please help me see what I can do to improve performance here?

I added this simple loop to draw() portion of a sketch:

ofSetColor( 55, 55, 55, 50);
ofSetLineWidth( 1 );

for(int i = 0; i < ofGetScreenHeight(); i += 10){
	for(int j = 0; j < ofGetScreenWidth(); j += 10){
		ofLine(j, i, j+value*10, i+value*10);
	}
}

As soon as I did that, the sketch has slowed to a crawl. I don’t understand why, since the sketch itself doesn’t have much going on at all. Here is a clip from the setup() through draw() portions:

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
ofPoint p;
// p = ofPoint( 100.0, 200.0 );
p.x = 100.0;
p.y = 200.0;

ofSetWindowShape( 1024, 768);

stem0 = ofPoint( 700, 100 );		//top
stem1 = ofPoint( 300, 140 );		//upper midpoint
stem2 = ofPoint( 350, 225 );		//lower midpoint
stem3 = ofPoint( 970, 710 );		//bottom
leftLeaf = ofPoint( 400, 520 );		//outer edge
rightLeaf = ofPoint( 600, 420 );	//outer edge

topSide = ofPoint( 50, 50 );
}

//--------------------------------------------------------------
void ofApp::update(){


}

//--------------------------------------------------------------
void ofApp::draw(){

//background
float time = ofGetElapsedTimef();
float value = sin( time * M_TWO_PI );
//defaults: map sin wave to -1 & 1, then map that 0 & 255
float v = ofMap( value, -2, 2, 0, 255 );
float vNeg = ofMap( value, -1, 1, 255, 0);
ofBackground( v, v, v );

cout << "v: " << v << "\n";
cout << "v/200: " << v/200 << "\n";
cout << "value: " << value << "\n";

ofSetColor( vNeg, vNeg, vNeg, 200 );
ofFill();
ofSetLineWidth( 1 );
ofSetCircleResolution( 100 );
ofEnableSmoothing();

ofLine( stem0, stem1 + v );
ofLine( stem2 + v, stem3 );

ofTriangle( (stem1 + v)-0.25, (stem2 + v)-0.25, leftLeaf * (v/200) );
ofTriangle( (stem1 + v)+0.25, (stem2 + v)+0.25, rightLeaf * (v/200) );

ofLine( rightLeaf * (v/200) , topSide );
ofLine( leftLeaf * (v/200) , topSide );

ofSetColor( 55, 155, 255);
ofNoFill();
ofCircle( stem0, 1 + ( 10 * (v/200) ) );

ofSetColor( 255, 55, 55);
ofLine( 970, 710, 980, 710 );
ofLine( 970, 710, 960, 710 );
ofLine( 970, 710, 970, 700 );
ofLine( 970, 710, 970, 720 );

ofSetColor( 55, 255, 55);
ofLine( 50, 50, 50, 60 );
ofLine( 50, 50, 50, 40 );
ofLine( 50, 50, 40, 50 );
ofLine( 50, 50, 60, 50 );

ofDrawBitmapStringHighlight( "THE STRUGGLER", 20, ofGetHeight() - 20 );
//ofDrawBitmapString( ofToString(value), 150, 748 );

}

You’re drawing about 7000 lines in that loop, which can be enough to cause a slow down.
Using primitives is not the most efficient way to draw on the screen, you should try ofMesh using lines as the primitive.
I don’t master this kind of thing, but you should’ve a look in the tutorials in the oF site.

(
http://openframeworks.cc/tutorials/graphics/generativemesh.html
http://openframeworks.cc/tutorials/graphics/opengl.html
)

Hope this helps!

Hi, as @cabral1349 mentions you’ll get much better performance using an ofMesh, it’s the difference between communicating with the graphics card like this:

"Hey, can you draw me a line from P1 to P2’
"Hey, can you draw me another line from P3 to P4’
“Hey, can you draw, etc etc”

And the super fast way:

“Hey, I have the data here for my 1000 lines, draw it!”

The code for that would look like this:

    ofMesh mesh;
    mesh.setMode( OF_PRIMITIVE_LINES );
    
    for(int i = 0; i < ofGetScreenHeight(); i += 10){
    	for(int j = 0; j < ofGetScreenWidth(); j += 10){
                mesh.addVertex( ofVec2f(j,i) );
                mesh.addVertex( ofVec2f(j+value*10, i+value*10) );
    
    	}
    }
    mesh.draw();

Guys

Thanks very much for the replies. Did a lot of reading and learned many things. I’m using an ofVboMesh, but its still slow when I try to update the vertices at each frame. Here are portions of the code:

ofApp.h

ofImage sketch;

int numVerts, meshCounter;

ofVboMesh mesh;

vector <ofVec2f> xLines, yLines;

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	ofSetWindowShape( 1024, 768);
	ofSetFrameRate( 30 );
	
	int h = ofGetScreenHeight();
	int w = ofGetScreenWidth();

	mesh.setMode(OF_PRIMITIVE_POINTS);
	mesh.setUsage(GL_STREAM_DRAW);
	mesh.enableColors();
	
	for ( int x = 0; x < w; x += 10 ) {
		for ( int y = 0; y < h; y += 10 ) {
			//ofVec2f pos( x, y );
			//mesh.addVertex( pos );
			ofPoint pointy( x, y );
			mesh.addVertex( pointy );
		}
	}

	numVerts = mesh.getNumVertices();
	meshCounter = 0;
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

	//background
	float time = ofGetElapsedTimef();
	float value = sin( time * M_TWO_PI );
	//defaults: map sin wave to -1 & 1, then map that 0 & 255
	float v = ofMap( value, -2, 2, 0, 255 );
	float vNeg = ofMap( value, -1, 1, 255, 0);
	ofBackground( v, v, v );
	
	ofSetColor( vNeg, vNeg, vNeg, 200 );
	ofFill();
	ofSetLineWidth( 1 );
	ofSetCircleResolution( 100 );
	ofEnableSmoothing();

	for ( int i = 0; i < numVerts; i += 1 ) {
		mesh.getVertices()[i].x += 1;
		mesh.getVertices()[i].y += 1;
		cout << mesh.getVertices()[i].x << endl;
		cout << mesh.getVertices()[i].y << endl;
		cout << i << endl;
	}
	mesh.draw();
	
	ofDrawBitmapStringHighlight( "THE STRUGGLER", 20, ofGetHeight() - 20 );
	



}

Hey @originalsurfmex

The first thing I noticed were the 3 ‘cout’ lines per iteration in the draw() - I get <2fps when I tested this on my own machine (recent MacBook Pro), and a nice 60fps when they are commented out.

Though it doesn’t seem necessary, the mesh’s vertices should probably be modified in the ‘update()’ function, setting a few global variables in the process to use with updating the background color and mesh draw colors, etc.

Also, I wasn’t familiar with GL_STREAM_DATA, so I looked it up and found this post, pointing to using GL_DYNAMIC_DRAW instead:

One final observation - I don’t think you need to be updating the vertices every frame if this is the final effect you’re going for - setting a translate would amount to the same thing:

offset++;  // move the 'drawing' coordinates down/over 1 pixel
ofPushMatrix()
ofTranslate( offset, offset);
mesh.draw();
ofPopMatrix()

Hope this is useful :smile:

Thanks very much for the detailed response! That answered my questions and then some. I was about to turn tail and run back to Processing.

Good idea about using translate too.

I had no idea that cout could have that effect. I tried googling around but found nothing. Do you know why it slows the sketch down?

I can’t give a good scientific reason off the bat (I don’t have a CS degree ;)), but when I imagine the sort of effort it’d take to update a log file with 23K lines every frame, it doesn’t seem all that surprising. That being said, it’d be interesting to see what the difference would be with a single call each iteration instead of three.

ok, couldn’t help but give it a go:
1 call ever iteration: 8.194 fps ( just outputting the vertex’s X )
2 calls: 2.69 fps ( one for the X, a second for the Y)
doing a single call with both X and Y: 4.08 fps

interesting!