1. Re: type string

Roderick Jackson <rjackson at CSIWEB.COM> wrote:

>BTW - Gabriel, I'm impressed by your string type, but... what made you
think
>to redefine the parameter? I didn't even know that that was possible in
>Euphoria  (and I'm not sure I like it, although in this case a definite
>speed benefit is there.)

Thanks for the compliment, Rod.  I stumbled into the idea for string12 back
when I posted "Type-check behavior."  Even now, the string routine I wrote
back then is *still* the all-time champion (for average-length strings, at
least):


global type string8(sequence s)
object x
   for i = 1 to length(s) do
      x = s[i]
      if integer(x) then
         if x < 0 then
            return 0
         elsif x > 255 then
            return 0
         end if
      else
         return 0
      end if
   end for
   return 1
end type


Now, you'll notice that you *can't* just pass any old object into it --
string8 will crash if you attempt to pass an atom to it, because of the
"sequence s" parm.  When this type-check function completely obliterated its
competition, I knew *something* was up.  My guess is, Euphoria can *really*
optimize the "s[i]" operation a lot better when "s" is defined as a
sequence.  (Of course, I didn't figure that out *right* away, which is why
my there's such a gap between string8 and string12. ;) )

What I did with string12 was start with string8 and swap "sequence s" and
"object x" around.  Then I added the sequence(x) check, and copied x to s,
before including the remainder of string8's code.  The result:


global type string12(object x)
sequence s
   if not sequence(x) then
      return 0
   end if
   s = x
   for i = 1 to length(s) do
      x = s[i]
      if integer(x) then
         if x < 0 then
            return 0
         elsif x > 255 then
            return 0
         end if
      else
         return 0
      end if
   end for
   return 1
end type


So, I didn't really think about "redefining the parameter" -- my main goal
was getting string8 to accept *any* object as a parm, while still keeping
its speed advantage.  The fact that string12 is *almost* as fast as string8
is, again, what leads me to believe that the "s[i]" operation is *greatly*
optimized when "s" is defined as a sequence.  (If I hadn't known that the "s
= x" operation in Euphoria just copies a pointer, I would never have even
tried this.)

But now that you mention it, I *do* see what disturbs you about the way
string12 "redefines the parameter."  Essentially, string12 takes the parm
variable, stuffs it into a private variable, then turns around and uses the
parm variable as fodder for the type-check algorithm!  Because of the way
this type evolved, I hadn't really noticed that before.  Hmm...well, it
works!  :)


Speaking to everyone now, I must say that this has been a *phenomenal*
learning experience for me -- in Euphoria, in general algorithmics, and in
benchmarking techniques.  Many thanks to everybody involved, including
Roderick Jackson, Larry Gregg, Daniel Berstein, Jeffrey Fielding, Carl R.
White, "Mike," Christopher D. Hickman, and especially Alan Tu for asking the
question that started it all.  This kind of thing, IMO, is what the list is
all about.  grin


Be seeing you,
   Gabriel Boehme


 -------
Good habit is necessary, bad habit is inevitable.

Robert Fripp
 -------

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu