Re: Optimizing multiple searches

new topic     » goto parent     » topic index » view thread      » older message » newer message
rneu said...

I have 2 lists (lines and terms) and I have to select out only those entries of 'lines' where all items of 'terms' are present. Above code works but can it be optimized or shortened since I see that functions such as match_all, find_all, find_each etc are also available?

(Not able to put hashes ## for code in above text).

Although there is a match_any() function in Euphoria it is not of the kind that works in this case. If there were you could've shortened the loop to:

for i=1 to length(lines) do 
   if match_any(terms, lines.i) = length(terms) then 
      outlist &= { lines.i } 
   end if 
end for 

You can easily write your own function, eg:

function match_any(sequence terms, sequence target) 
   sequence ret = {} 
   for i=1 to length(terms) do 
      integer n = match(terms.i, target) 
      if n then ret &= n end if 
   end for 
   return ret 
end function 

But the first thing to ask really is what are the data set sizes and what optimization (and other programming) goals do you have? Maybe when you say "optimize" you really mean beautify? Or is it lowest number of lines of code? Or is it maximum performance? There are compromises or trade-offs to be made in every choice. But the very first goal you must strive for is, of course, correctness. If it works correctly and you can live with the performance.. you're done!

Spock

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

Search



Quick Links

User menu

Not signed in.

Misc Menu