Page 1 of 1

Slower or faster? ;-)

Posted: Fri Feb 04, 2005 9:35 am
by Chris Corbyn

Code: Select all

var differenceVar = var1 - var2
if (differenceVar < 0) &#123;
    differenceVar = 0
&#125;
I'm trying to do a subtraction but make it just end up as zero if was a negative value... taking the modulus is no good because the idea is that a series of values a decreasing exponentially but one rule is that they cannot go below zero (and occassionally they do due to slight errors).

The reason I need another way is that i have a function with a loop that performs this operation on several variables thousands of times and it's a bit slow so anything better than the above would speed it up dramatically.

Thanks :-)

Posted: Mon Feb 07, 2005 9:28 am
by Chris Corbyn
I came across this function... Math.max(val1, val2) -> Returns the maximum of the two values.

I've implemeted it like this

Code: Select all

differenceVar = Math.max(0, (var1 - var2))
I'm not positive but I think it's got slower... then again I'm testing it on a slower machine.

I always though this type if thing would run faster than specifying conditions and overwriting vars or am I all wrong here?

Thanks