1. ABS --fastest thus far
testing proposed code thus far, the two finalists are:
--carl's proposed abs, beat jiri's
function ABScarl(object x)
return x*((x>0)-(x<0))
end function
--an alternative proposition by carl, with optimization
--seems compare is much faster than (x>0)-(x<0)
function ABSobj(object x)
if not atom(x) then return x*compare({x},{0}) end if
if x<0 then return -x else return x end if
end function
and their benchmarks are:
time ABScarl using atoms:1.480000
time ABSobj using atoms:1.150000
time ABScarl using seq's:2.690000
time ABSobj using seq's:0.930000
therefore:ABSobj is faster for both atoms and sequences.
(about 20% for atoms, and 250% for seq...)
also note:
function ABSatom(atom x)
if x<0 return -x else return x end if
end function
is even faster than ABSobj if you *know* you only need atoms
mebbe ABSatom and ABSobj should both be in your toolbox??
/* copies of prog used to benchmark I wrote are available
if you realllllllyyyyyyyy doubt me that much :)
you're welcome to make your own benchmarks as well :)
*/
can anyone beat carl's compare({x},{0}) suggestion?
can anyone beat
if (x<0) then return -x else return x end if
for atoms?
happy thinking --Hawke'
2. Re: ABS --fastest thus far
- Posted by Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL>
Sep 01, 1998
-
Last edited Sep 02, 1998
>can anyone beat carl's compare({x},{0}) suggestion?
May I notice compare ({x}, {0}) only has the same effect as (x>0)-(x<0) for
atoms.
For sequences it does *not* work. You need to use: (x>0)-(x<0)
-- Time this one.. its a variant of Carl's way, I would think this one is
the fastest.
function abs (object x)
return x + ((x < 0) + (x < 0) * x)
end function
And has any one actually timed, the manual recursive method:
I know its slower, but just how how much ?
function abs (object x)
if sequence (x) then
for index = 1 to length(x) do
x[index] = abs (x[index])
end for
else
if x < 0 then return -x else return x end if
end if
end function
>can anyone beat
> if (x<0) then return -x else return x end if
>for atoms?
Dont think thats possible..
Ralf
3. Re: ABS --fastest thus far
- Posted by Hawke <mdeland at NWINFO.NET>
Sep 02, 1998
-
Last edited Sep 03, 1998
Ralf Nieuwenhuijsen wrote:
>>can anyone beat carl's compare({x},{0}) suggestion?
> May I notice compare ({x}, {0}) only has the same effect as (x>0)-(x<0) for
> atoms.
> For sequences it does *not* work. You need to use: (x>0)-(x<0)
errrrr.... *blush*
yer right... oopsie!
see other post for my bestest solution (abs4)
4. Re: ABS --fastest thus far
>Ralf Nieuwenhuijsen wrote:
>>>can anyone beat carl's compare({x},{0}) suggestion?
>> May I notice compare ({x}, {0}) only has the same effect as (x>0)-(x<0)
for atoms.
>> For sequences it does *not* work. You need to use: (x>0)-(x<0)
>errrrr.... *blush*
>yer right... oopsie!
>see other post for my bestest solution (abs4)
Im not trying to brake your world, but try abs4 with this sequence: { { { 1,
1, 1}, 0, 0}, 34, }
Yours only works with one dimensional sequences.
We were kinda looking for one that did all the tricks together.. atoms, any
type of sequence, integers, etc.
If recursion works so well, lets try this one..
It is a bunch of code though.. so it doesnt win the 'simplicity' contest..
but maybe the speed..
Or is recursion by default slower than jumping to another function ?
(a difference of more than a sequence () call ?)
-- ABS.e
object temp
function abs_sequence (sequence x)
for index = 1 to lenght(x) do
temp = index[x]
if sequence(temp) then
index[x] = abs_sequence (temp)
elsif temp < 0 then
index[x] = -temp
end if
end for
end function
global function abs (object x)
if sequence (x) then
for index = 1 to length(x) do
temp = index[x]
if sequence(temp) then
index[x] = abs_sequence (temp)
elsif temp < 0 then
index[x] = -temp
end if
end for
elsif x< 0 then
return -x
else
return x
end if
end function