1. findany_from()

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

2. Re: findany_from()

Larry Miller wrote:
> 
> I have been experimenting with this function found in SourceForge SVN. I have
> a smaller, simpler, and faster version:

Larry,

This is great! Another function already cleaned up and made faster. I've
committed your 1st version of findany_from to SVN.

Thanks Larry!

--
Jeremy Cowgar
http://jeremy.cowgar.com

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

3. Re: findany_from()

The use of magic numbers like this makes me uncomforatable.
In ten years we will be wondering what a program
doesn't work when sequences get to be more than a billion
atoms long.  Well, probably it will never be a problem
but why not use length(haystack)+1 instead of 1e9?

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


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

Search



Quick Links

User menu

Not signed in.

Misc Menu