1. all() and any() routines

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 message » categorize

2. Re: all() and any() routines

>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.
>

I recieved the message this morning. March 21 9:13 am EST EastCoast.

new topic     » goto parent     » topic index » view message » categorize

3. Re: all() and any() routines

--=====================_859055508==_


>Poster:       Ad Rienks <Ad_Rienks at COMPUSERVE.COM>
>Subject:      all() and any() routines
>-------------------------------------------------------------------------------
>
>Jef Zeitlin provided us with some very nice routines, all() and any().

>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?
        I ran some tests. The results follow. My conclusion, it all depends.
If someone can direct me to a fine grained timer, I will re-run. The 200000
results suffer from coarse timer resolution. Art Adamson
------------------------------------------------------------------------------

--=====================_859055508==_


        The following data shows relative  speed  for Mr Zeitlin's loop
      based any and all functions vs Mr. Rienk's find based any and all
      which was offered as hopefully faster.  My answer, they are about
      the  same  with  some exceptions....asssuming  I have implemented
      them properly.  My versions follow  in the include  file I used.

      The 200000 versions results suffer from lack of timer resolution.
      Is there a better timer available for Euphoria, I can easily retest.
      Art Adamson,           euclid at horandata.net

      INCLUDE FILE:

--2 versions of any and all for speed comparison tests
--Art Adamson, euclid at horandata.net

--loop version by Zeitlin

global function allloop(object o)
if atom(o) then return o end if
for i = 1 to length(o) by 1 do
  if not o[i] then return 0 end if
end for
  return 1
end function   --allloop()


global function anyloop(object o)
if atom(o) then return o end if
for i = 1 to length(o) by 1 do
  if  o[i] then return 1  end if
end for
  return 0
end function   --anyloop()


--find version by Rienks

global function all(object o)
        if atom(o) then return o end if
        if find(0,o) then return 0
        else
                return 1
        end if
end function   --all()

global function any(object o)
        if atom(o) then return o end if
        if find(1,o) then return 1
        else
                return 0
        end if

end function   --any()






      END INCLUDE FILE

|||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||


Test of loop vs find anyall.e for FLOATS and sequence length :200000
Time for all/any with-find...with-loop are shown thus,  0.66...0.65
Using a pentium, 100/120 MHZ, 30 Meg Ram...
**Test of All********************************************
      The sequence is filled with 3.22345.....,,,,,,,.......find...loop
Using      all(s < 2.22345),  the answer is:  No,  time is: 0.11
Repeat above test to test cache effects
Using      all(s < 2.22345),  the answer is:  No,  time is: 0.16...0.11
Using      all(s > 2.22345),  the answer is:  Yes, time is: 0.17...0.22
Using      all(s > 4.22345),  the answer is:  No,  time is: 0.11...0.11
Using      all(s >=4.22345),  the answer is:  No,  time is: 0.16...0.11
Using      all(s <= 4.22345), the answer is:  Yes, time is: 0.17...0.16
**Test of Any********************************************
Using      any(s > 4.22345),  the answer is:  No,  time is: 0.17...0.22
Using      any(s < 4.22345),  the answer is:  Yes, time is: 0.11...0.16
Using      any(s < 2.22345),  the answer is:  No,  time is: 0.11...0.22
Using      any(s = 4.22345),  the answer is:  No,  time is: 0.11...0.16
Using      any(s >= 4.22345), the answer is:  No,  time is: 0.17...0.16
Using      any(s <= 4.22345), the answer is:  Yes, time is: 0.16..,0.11
|||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||


Test of loop vs find anyall.e for FLOATS and sequence length :2000000
Time for all/any with-find...with-loop are shown thus,  0.66...0.65
Using a pentium, 100/120 MHZ, 30 Meg Ram...
**Test of All********************************************
      The sequence is filled with 3.22345.....,,,,,,,.......find...loop
Using      all(s < 2.22345),  the answer is:  No,  time is: 1.37
Repeat above test to test cache effects
Using      all(s < 2.22345),  the answer is:  No,  time is: 1.32...1.26
Using      all(s > 2.22345),  the answer is:  Yes, time is: 1.48...1.98
Using      all(s > 4.22345),  the answer is:  No,  time is: 1.32...1.32
Using      all(s >=4.22345),  the answer is:  No,  time is: 1.26...1.32
Using      all(s <= 4.22345), the answer is:  Yes, time is: 1.43...1.98
**Test of Any********************************************
Using      any(s > 4.22345),  the answer is:  No,  time is: 1.43...2.03
Using      any(s < 4.22345),  the answer is:  Yes, time is: 1.26...1.32
Using      any(s < 2.22345),  the answer is:  No,  time is: 1.43...2.03
Using      any(s = 4.22345),  the answer is:  No,  time is: 1.43...1.98
Using      any(s >= 4.22345), the answer is:  No,  time is: 1.48...1.98
Using      any(s <= 4.22345), the answer is:  Yes, time is: 1.26..,1.32
|||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||||||


Test of loop vs find anyall.e for FLOATS and sequence length :4000000
Time for all/any with-find...with-loop are shown thus,  0.66...0.65
Using a pentium, 100/120 MHZ, 30 Meg Ram...
**Test of All********************************************
      The sequence is filled with 3.22345.....,,,,,,,.......find...loop
Using      all(s < 2.22345),  the answer is:  No,  time is: 3.41
Repeat above test to test cache effects
Using      all(s < 2.22345),  the answer is:  No,  time is: 2.58...2.58
Using      all(s > 2.22345),  the answer is:  Yes, time is: 2.91...4.01
Using      all(s > 4.22345),  the answer is:  No,  time is: 2.64...2.58
Using      all(s >=4.22345),  the answer is:  No,  time is: 2.58...2.64
Using      all(s <= 4.22345), the answer is:  Yes, time is: 2.91...3.96
**Test of Any********************************************
Using      any(s > 4.22345),  the answer is:  No,  time is: 2.91...4.01
Using      any(s < 4.22345),  the answer is:  Yes, time is: 2.58...2.58
Using      any(s < 2.22345),  the answer is:  No,  time is: 2.91...4.12
Using      any(s = 4.22345),  the answer is:  No,  time is: 2.91...3.96
Using      any(s >= 4.22345), the answer is:  No,  time is: 2.91...3.95
Using      any(s <= 4.22345), the answer is:  Yes, time is: 2.58..,2.64
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII


Test of loop vs find anyall.e for INTEGERS and sequence length :200000
Time for all/any with-find...with-loop are shown thus,  0.66...0.65
Using a pentium, 100/120 MHZ, 30 Meg Ram...
**Test of All********************************************
      The sequence is filled with 3 .....,,,,,,,.......find...loop
Using      all(s < 2 ),  the answer is:  No,  time is: 0.06
Repeat above test to test cache effects
Using      all(s < 2 ),  the answer is:  No,  time is: 0.05...0.06
Using      all(s > 2 ),  the answer is:  Yes, time is: 0.05...0.16
Using      all(s > 4 ),  the answer is:  No,  time is: 0.06...0.05
Using      all(s >=4 ),  the answer is:  No,  time is: 0.06...0.05
Using      all(s <= 4 ), the answer is:  Yes, time is: 0.06...0.16
**Test of Any********************************************
Using      any(s > 4 ),  the answer is:  No,  time is: 0.06...0.11
Using      any(s < 4 ),  the answer is:  Yes, time is: 0.05...0.06
Using      any(s < 2 ),  the answer is:  No,  time is: 0.11...0.11
Using      any(s = 4 ),  the answer is:  No,  time is: 0.05...0.17
Using      any(s >= 4 ), the answer is:  No,  time is: 0.05...0.11
Using      any(s <= 4 ), the answer is:  Yes, time is: 0.06..,0.05
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII


Test of loop vs find anyall.e for INTEGERS and sequence length :2000000
Time for all/any with-find...with-loop are shown thus,  0.66...0.65
Using a pentium, 100/120 MHZ, 30 Meg Ram...
**Test of All********************************************
      The sequence is filled with 3 .....,,,,,,,.......find...loop
Using      all(s < 2 ),  the answer is:  No,  time is: 0.66
Repeat above test to test cache effects
Using      all(s < 2 ),  the answer is:  No,  time is: 0.55...0.55
Using      all(s > 2 ),  the answer is:  Yes, time is: 0.71...1.27
Using      all(s > 4 ),  the answer is:  No,  time is: 0.6...0.55
Using      all(s >=4 ),  the answer is:  No,  time is: 0.55...0.55
Using      all(s <= 4 ), the answer is:  Yes, time is: 0.71...1.27
**Test of Any********************************************
Using      any(s > 4 ),  the answer is:  No,  time is: 0.71...1.26
Using      any(s < 4 ),  the answer is:  Yes, time is: 0.55...0.55
Using      any(s < 2 ),  the answer is:  No,  time is: 0.72...1.26
Using      any(s = 4 ),  the answer is:  No,  time is: 0.71...1.27
Using      any(s >= 4 ), the answer is:  No,  time is: 0.71...1.26
Using      any(s <= 4 ), the answer is:  Yes, time is: 0.55..,0.55
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII


Test of loop vs find anyall.e for INTEGERS and sequence length :4000000
Time for all/any with-find...with-loop are shown thus,  0.66...0.65
Using a pentium, 100/120 MHZ, 30 Meg Ram...
**Test of All********************************************
      The sequence is filled with 3 .....,,,,,,,.......find...loop
Using      all(s < 2 ),  the answer is:  No,  time is: 1.76
Repeat above test to test cache effects
Using      all(s < 2 ),  the answer is:  No,  time is: 1.16...1.1
Using      all(s > 2 ),  the answer is:  Yes, time is: 1.42...2.53
Using      all(s > 4 ),  the answer is:  No,  time is: 1.15...1.1
Using      all(s >=4 ),  the answer is:  No,  time is: 1.16...1.1
Using      all(s <= 4 ), the answer is:  Yes, time is: 1.42...2.53
**Test of Any********************************************
Using      any(s > 4 ),  the answer is:  No,  time is: 1.43...2.47
Using      any(s < 4 ),  the answer is:  Yes, time is: 1.15...1.1
Using      any(s < 2 ),  the answer is:  No,  time is: 1.43...2.53
Using      any(s = 4 ),  the answer is:  No,  time is: 1.48...2.48
Using      any(s >= 4 ), the answer is:  No,  time is: 1.42...2.48
Using      any(s <= 4 ), the answer is:  Yes, time is: 1.15..,1.1
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

--=====================_859055508==_

Arthur P. Adamson, The Engine Man, euclid at mail.horandata.net

--=====================_859055508==_--

new topic     » goto parent     » topic index » view message » categorize

4. Re: all() and any() routines

Art Adamson writes:
>  Is there a better timer available for Euphoria, I can easily retest.

In Euphoria version 1.5 you can call tick_rate(100) to get
a resolution of .01 seconds in the time() function.
Normally the resoultion is about .055 seconds, or tick_rate(18.2).
You might want to go as high as (say) tick_rate(500) for even
better resolution. Beyond 1000 the machine will likely be spending
a few percent of its time handling clock interrupts, and you might see
an (ignorable) suggestion from Windows 95 that you should
run the program in pure DOS 7.0 mode.

Of course the low-tech solution to poor timer resolution is just
to run the benchmark code in a loop (say) 100 times, and then
divide the resulting times by 100.

Regards,
  Rob Craig
  Rapid Deployment Software

new topic     » goto parent     » topic index » view message » categorize

5. Re: all() and any() routines

>In Euphoria version 1.5 you can call tick_rate(100) to get
>a resolution of .01 seconds in the time() function.
I'm ready to buy, tell me how. Bye Art
Arthur P. Adamson, The Engine Man, euclid at mail.horandata.net

new topic     » goto parent     » topic index » view message » categorize

6. Re: all() and any() routines

>In Euphoria version 1.5 you can call tick_rate(100) to get
>a resolution of .01 seconds in the time() function.
I'm ready to buy, tell me how. Bye Art
Arthur P. Adamson, The Engine Man, euclid at mail.horandata.net

You should buy it, but in the meantime you can download it from the
Official Euphoria Homepage, and already use it.

Ad Rienks.

Hi Art,

I really appreciate what you did in testing my code against that by Jeff
Zeitlin. He has send me a note that he has seen my point and is going to
update his code along the lines I suggested. But there is still one thing
that keeps me wondering: can you, or maybe somebody else, explain the
differences in speed when testing for '<, >, <=, >= and ='  ?
Could this be inexactness that can be eliminated by setting tick_rate()
higher?
Or is it something else? Are you running your program under Windows 95? In
that case, maybe the Windows shell is also consuming time. I always run
Euphoria programs from the DOS prompt. I myself will also test this again
using tick_rate.

BTW: Jeff said I should not complain about the speed of 100000 comparisons
being done in tenths of seconds. Well, I don't. In fact, I keep being
surprised with the speed at which Euphoria is doing calculations and
comparisons on my now already 'oldfashioned' P75. Compliments again to RDS.

=================================================================
Ad Rienks       AdRienks at compuserve.com

Does anyone have a suggestion for a nice signature?

new topic     » goto parent     » topic index » view message » categorize

7. Re: all() and any() routines

> But there is still one thing
>that keeps me wondering: can you, or maybe somebody else, explain the
>differences in speed when testing for '<, >, <=, >= and ='  ?
>Could this be inexactness that can be eliminated by setting tick_rate()
>higher?
        I don't think the tick rate is responsible since the effect is
always there. Re explanation, I'm waiting/expecting some of our colleagues
to explain/fix the discrepance.

>Or is it something else? Are you running your program under Windows 95? In
>that case, maybe the Windows shell is also consuming time. I always run
>Euphoria programs from the DOS prompt. I myself will also test this again
>using tick_rate.
        I use DOS 6.22
        Bye Art
Arthur P. Adamson, The Engine Man, euclid at mail.horandata.net

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu