1. Re: type string

I've tested three string routines with this benchmark. I first tested without
all the checks to make sure s is a sequence and that it holds all integers, and
the string2 won, but then I added the checks and string3 won.
global type string1(object s)
 integer i
 if not sequence(s) then
     return 0
 end if
 i = length(s)
 if (i=0) or (integer(s[i]) and (s[i]>0) and (s[i]<=255) and
     string1(s[1..i-1])) then
     return 1
 end if
 return 0
end type
global type string2(object s)
    if not sequence(s) then
 return 0
    end if
    for i = 1 to length(s) do
 if not integer(s[i]) then
     return 0
 end if
    end for
    if find(0,(s > 0) and (s <= 255)) then
 return 0
    end if
    return 1
end type
global type string3(object s)
    if not sequence(s) then
 return 0
    end if
    for i = 1 to length(s) do
 if not integer(s[i]) then
     return 0
 end if
 if s[i] < 0 or s[i] > 255 then
     return 0
 end if
    end for
    return 1
end type
atom t1, t2, t3
integer s
t1 = time()
for i = 1 to 10000 do
    s = string1("hello")
    if s != 1 then
 puts(1,"string1 doesn't work.\n")
 exit
    end if
    s = string1({0,2,-1})
    if s != 0 then
 puts(1,"string1 doesn't work.\n")
 exit
    end if
end for
t1 = time() - t1
t2 = time()
for i = 1 to 10000 do
    s = string2("hello")
    if s != 1 then
 puts(1,"string2 doesn't work.\n")
 exit
    end if
    s = string2({0,2,-1})
    if s != 0 then
 puts(1,"string2 doesn't work.\n")
 exit
    end if
end for
t2 = time() - t2
t3 = time()
for i = 1 to 10000 do
    s = string3("hello")
    if s != 1 then
 puts(1,"string3 doesn't work.\n")
 exit
    end if
    s = string3({0,2,-1})
    if s != 0 then
 puts(1,"string3 doesn't work.\n")
 exit
    end if
end for
t3 = time() - t3
? t1
? t2
? t3


--
Jeffrey Fielding
JJProg at cyberbury.net
http://members.tripod.com/~JJProg/

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu