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 & HelpSyntax Questions › scale() vs. multiplication
Page Index Toggle Pages: 1
scale() vs. multiplication (Read 543 times)
scale() vs. multiplication
Sep 28th, 2006, 3:40am
 
So I've written a pentagon function for drawing regular pentagons. The vertex values are for a unit pentagon and then you just pass it a scale factor. It currently looks like this:

Code:

void pent(float r)
{
pushMatrix();
scale(r);
beginShape(LINE_LOOP);
vertex(0, 1);
vertex(0.951056516f, 0.309016994f);
vertex(0.587785252f, -0.809016994f);
vertex(-0.587785252f, -0.809016994f);
vertex(-0.951056516f, 0.309016994f);
endShape();
popMatrix();
}


Would it be better to do it like this?

Code:

void pent(float r)
{
beginShape(LINE_LOOP);
vertex(0, r);
vertex(r*0.951056516f, r*0.309016994f);
vertex(r*0.587785252f, r*-0.809016994f);
vertex(r*-0.587785252f, r*-0.809016994f);
vertex(r*-0.951056516f, r*0.309016994f);
endShape();
}


In other words, are the push, pop and scale functions not expensive enough for me to worry about?
Re: scale() vs. multiplication
Reply #1 - Oct 2nd, 2006, 2:21pm
 
You can test the speed of anything with the following format:
Code:

int timer = 0;
void setup(){
timer = millis();
for(int i = 0; i < 1000; i++){
// function 1
}
println(millis() - timer);
timer = millis();
for(int i = 0; i < 1000; i++){
// function 2
}
println(millis() - timer);
}

eg:
Code:

int timer = 0;
void setup(){
timer = millis();
for(int i = 0; i < 1000; i++){
pent(10);
}
println(millis() - timer);
timer = millis();
for(int i = 0; i < 1000; i++){
pent2(10);
}
println(millis() - timer);
}

void pent(float r)
{
pushMatrix();
scale(r);
beginShape(LINE_LOOP);
vertex(0, 1);
vertex(0.951056516f, 0.309016994f);
vertex(0.587785252f, -0.809016994f);
vertex(-0.587785252f, -0.809016994f);
vertex(-0.951056516f, 0.309016994f);
endShape();
popMatrix();
}

void pent2(float r)
{
beginShape(LINE_LOOP);
vertex(0, r);
vertex(r*0.951056516f, r*0.309016994f);
vertex(r*0.587785252f, r*-0.809016994f);
vertex(r*-0.587785252f, r*-0.809016994f);
vertex(r*-0.951056516f, r*0.309016994f);
endShape();
}

It seems that scale() takes a hell of a lot longer. It's a much more useful command to call at the beginning of drawing a sketch (to deal with a different resolution for instance) than to use inside a method.
Re: scale() vs. multiplication
Reply #2 - Oct 2nd, 2006, 4:53pm
 
I thought as much. Thanks for taking the time to time it. I'm just too lazy to try such things.
Page Index Toggle Pages: 1