Pages

Monday, January 18, 2010

Bigger/Smaller Comparisons

Well, it's about MaxScript comparision topic. I wonder why people continue to write custom funcions like this:
fn bigVal v1 v2 = if v1 > v2 then v1 else v2
fn smallVal v1 v2 = if v1 < v2 then v1 else v2

...provided that Max has great built functions amax() and amin().
Even more, they take more than 2 arguments and/or array.
-- Examples:
myMin1 = amin #(5,1,4,2,8)
myMin2 = amin 5 1 4 2 8 -- same as above
myMin1 == myMin2 --> true

In plain speed test both ((bigVal v1 v2) and (amax v1 v2), or (smallVal v1 v2) and (amin v1 v2)) has equal runtime. Well, even this is enough to skip writing of such functions, right? I know many people with a nice reputation in coding which still continue to do just that :) ... but not to digress from the topic... instead, let see the strong hand of those functions by simple test:
-- Let's fill an array with 100000 random integers
testArray = for i=1 to 100000 collect random 1 100

-- test 1
timeStart = timestamp()
amin testArray -- find min
amax testArray -- find max
timeEnd = timestamp()
t1 = (timeEnd - timeStart)

-- test 2
timeStart = timestamp()
sort testArray -- 1st need to sort the array
testArray[1] -- find min
testArray[testArray.count] -- find max
timeEnd = timestamp()
t2 = (timeEnd - timeStart)

format "Test 1 Time:\t\t% ms.\n" t1
format "Test 2 Time:\t\t% ms.\n" t2

-- result:
Test 1 Time: 0 ms.
Test 2 Time: 31 ms.

So, let's say, end of story here :)

No comments:

Post a Comment

Thanks for your comment