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.
Page Index Toggle Pages: 1
max() (Read 1698 times)
max()
Oct 4th, 2006, 9:10pm
 
I was writing my library and decided to test if there was a faster way of doing abs().

return a < 0 ? -a : a;

The above tests twice as fast as Math.abs(). I'd thought I'd check the PApplet code before bragging but you'd already beat me to it and implemented this.

http://dev.processing.org/source/index.cgi/tags/processing-0118/core/src/processing/core/PApplet.java?rev=2709&view=markup

But why haven't you done the same with max()?

Code:

int timer = 0;
float a = 3.0;
float b = 4.0;

void setup(){
timer = millis();
for(int i = 0; i < 10000000; i++){
// function 1
float x = (float)Math.max(a, b);
}
println(millis() - timer);
timer = millis();
for(int i = 0; i < 10000000; i++){
// function 2
float x = nMax(a, b);
}
println(millis() - timer);
}

float nMax(float a, float b){
return a > b ? a : b;
}

The latter method seems more than twice as fast even as a function. I wouldn't normally quibble, but since you've already optimised abs(), what's the deal with max() or min()?
Re: max()
Reply #1 - Oct 5th, 2006, 8:25pm
 
hm, we do for int, but i don't recall why we don't for float. i don't have a note about it in the code so i guess it was an oversight, or i didn't think the overhead was as bad for floats as ints. but if that's the case, then i'll switch it.
Page Index Toggle Pages: 1