all() and any() routines

new topic     » topic index » view thread      » older message » newer message

Jef Zeitlin provided us with some very nice routines, all() and any().

global function all(object o)
    if atom(o) then
        return o        -- that is the letter, not 0(ZERO)
    end if
    for i = 1 to length(o) do
        if not o[i] then
            return 0    -- this *is* a ZERO
        end if
    end for
    return 1
end function    -- all()
--------------------------------------------------------
global function any(object o)
    if atom(o) then
        return o
    end if
    for i = 1 to length(o) do
        if i[o] then
            return 1
        end if
    end for
    return 0
end function    -- any
--------------------------------------------------------

I've been checking these out, but it seems to me you can speed them up (at
least in a lot of cases), by replacing the loops 'for..do /if..then... end
if /end for' with a simple 'if find(0, o) then...' and 'if find(1, o)
then...'.
The sequence o, that is used inside the routines, is of the same length as
the sequence that is passed to the routines, but it only contains
0's(FALSE) and 1's(TRUE).
It seems to me that reading the sequence using 'find' is more elegant and
probably quicker than using a loop inside a loop.
I tested my suggested improvement to find the square of 99999 in a
100000-element sequence of squares. (Pretty useless again). In this case it
took my Pentium 75 with 8Mb 0.16 seconds, whereas Jef's code took twice as
much.
But when I began testing it with 200000 and 300000 elements, the speed
difference decreased, I think maybe because most of the time was spend
writing to and reading from the hard disk.
Can someone with more RAM test this?

Sincerely yours,

Ad.

PS. I wander how long this message will take to get to the Listserver. Last
time it took 2 whole days. I'm posting this one now on March 20. In the USA
(Westcoast) it should be just 3 a.m.

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu