Re: type string
--=====================_921217325==_
Here's my record breaking string type check:
global type string5(object s)
object c
if not sequence(s) then
return 0
end if
for j = 1 to length(s) do
c = s[j]
if sequence(c) or floor(c/255) or not c then
return 0
end if
end for
return 1
end type
Attached is the source of my benchmark code. It's open to add new routines
for measurment, look on source where to put your type definition and where
to "hook" it to the benchmark (near the end of the file).
Please someone running plain DOS verify my results, Windows NT seem to not
like too much tick_rate(). I had to use ridiculus values (1.000.000) to
obtain cuantifiable values! I suggest as a reasonable test 100.000
iterations 5 times (ex strbench 100000 5).
--=====================_921217325==_
-- Default iterations, multipier and test string can be overriden from the
-- command line:
-- ex strbench i m string
-- Each type function is called i*m times.
include get.e
include machine.e
sequence str, ids, t, cl
integer iterations, multiplier, cluster
object x
--*****************************************************
-- global type your_string_type_here()
-- return
-- end type
--*****************************************************
global type string1(object s)
--Author: Daniel Berstein
--Slow recursive version
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)
--Author: ?
--Very fast
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)
--Author: ?
--Quite fast
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
global type string4(object s)
--Author: Daniel Berstein
--Almost the fastest
integer i
object c
if not sequence(s) then
return 0
end if
i=length(s)
for j = 1 to i do
c = s[j]
if sequence(c) or floor(c/255) or not c then
return 0
end if
end for
return 1
end type
global type string5(object s)
--Author: Daniel Berstein
--The fastest... small optimization of string4
object c
if not sequence(s) then
return 0
end if
for j = 1 to length(s) do
c = s[j]
if sequence(c) or floor(c/255) or not c then
return 0
end if
end for
return 1
end type
procedure results(sequence times)
--Sort and display results
sequence v, r
integer len, k
len = length(times)
v = repeat({0,0}, len)
for i = 1 to len do
v[i][1] = i
v[i][2] = times[i]/multiplier
end for
r = repeat({0,0}, len)
for i = len to 1 by -1 do
for j = 1 to length(v) do
if v[j][2] >= r[i][2] then
r[i] = v[j]
k = j
end if
end for
v = v[1..k-1]&v[k+1..length(v)]
end for
printf(1, "\tAverage time per type for %d iterations:\n\n", {iterations})
for i = 1 to len do
printf(1, "\tstring%d: %f seconds\n", {r[i][1], r[i][2]})
end for
end procedure
function test(atom id)
--Cluster test loop
integer i
atom t
t = time()
for k = 1 to cluster do
i = call_func(id, {str})
end for
return time()-t
end function
procedure run()
-- Default test values
iterations = 10000
multiplier = 1
str = "1234567890abcdefghijklmnopqrstuvwxyz"
-- Read command line
cl = command_line()
if length(cl) > 2 then
x = value(cl[3])
if x[1] = GET_SUCCESS and integer(x[2]) then
--Override iterations
iterations = x[2]
end if
if length(cl) > 3 then
x = value(cl[4])
if x[1] = GET_SUCCESS and integer(x[2]) then
--Overrride multiplier
multiplier = x[2]
end if
end if
if length(cl) > 4 then
--Override test string
str = cl[5]
for i = 6 to length(cl) do
str = str & " " & cl[i]
end for
end if
end if
--Set better resolution. Seems that NT is fooling with tick rates!
--Change this phenomenal tick rate if it burns your PC.
tick_rate(1000000)
--Break iterations into clusters to avoid "lost" ticks errors.
cluster=iterations/100
--Main test loop
printf(1, "Performing %d iterations %d times.\nTest string: %s\n" &
"Please wait...\n\n", {iterations, multiplier, str})
x = floor(iterations/cluster)
t = repeat(0, length(ids))
for i = 1 to multiplier do
for j = 1 to x do
for k = 1 to length(t) do
t[k] += test(ids[k])
end for
end for
end for
--Display results
results(t)
end procedure
--Build routine_id's sequence
ids = {}
ids = append(ids, routine_id("string1"))
ids = append(ids, routine_id("string2"))
ids = append(ids, routine_id("string3"))
ids = append(ids, routine_id("string4"))
ids = append(ids, routine_id("string5"))
--*****************************************************
-- ids = append(ids, routine_is(your_string_type_here))
--*****************************************************
run()
--=====================_921217325==_
Regards,
Daniel Berstein
[daber at pair.com]
--=====================_921217325==_--
|
Not Categorized, Please Help
|
|