findany_from()

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

I have been experimenting with this function found in SourceForge SVN. I have a
smaller, simpler, and faster version:
global function findany_from(sequence needles, sequence haystack, integer start)
    for i = start to length(haystack) do
    	if find(haystack[i],needles) then
    	    return i
    	end if
    end for
    return 0
end function


Another option was mentioned in the TODO section. I wrote a version using
find_from() to search the haystack.

global function findany_from2(sequence needles, sequence haystack, integer
start)
integer index,result
    if start > length(haystack) then
        return 0
    end if
    result=1e9
    for i=1 to length(needles) do
    	index=find_from(needles[i],haystack,start)
    	if index then
    		if index<result then
    			result=index
    		end if
    	end if
    end for
    if result<1e9 then
    	return result
    else
    	return 0
    end if
end function


There is no clear winner when comparing performance. Either can be much faster
than the other, depending on the nature of the data. The first is a clear winner
in terms of simplicity and clarity.

Larry Miller

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

Search



Quick Links

User menu

Not signed in.

Misc Menu