To compare the contents of two unsorted arrays, it's always an O(n^2)
operation. That is, what you are doing is the fastest /type/ of algorithm
available.
However, there are things that you can do to speed it up.
First of all, always break out of the inner loop on a match. If you are not
looking for multiple matches, you don't need to keep going. If you are
looking for multiple matches, this will only find them in one direction
anyway and you need to look for another solution. Of course, by extension,
if you only need to find one match, break out of /both/ loops on a match.
Second, if you are looking to find if a specific element of the first array
is contained in the second, find the index of the element in the first
array, then loop thru the second. However, that's likely to be of little
help since it looks like you're counting matches or displaying them or
something of that sort.
I assume that CFMX will optimize the 'o LT arrayLen(ary1)' bits of the for
loops to a static value. However, I find it unlikely that the inner one
will "stick" across steps in the outside one, so to squeese a bit of
performance there, you might want to assign arraylen(ary2) to a variable and
reference it instead of calling arraylen(ary2) once for each element in
ary1. And, of course, if someone more knowledgable than me says it will
stick, don't bother; if someone more knowledgable than me says it's not
optimized to a static at all, assign both arraylen() results to variables.
Of course, this is all assuming that you're not altering the length of the
arrays in your loop. :-)
Finally, a short note: if you need case-sensitive comparisons, don't use EQ
or IS. At least in CF5 they are case-insensitive. AFAIK, if you need
case-sensitivity, you'll have to build your own comparitor (I'd just write a
UDF) to do it. But I may be wrong about that.
HTH.
--Ben Doom
Programmer & General Lackey
Moonbow Software
: -----Original Message-----
: From: jon hall [mailto:jonhall@;ozline.net]
: Sent: Monday, November 11, 2002 3:15 PM
: To: CF-Talk
: Subject: fast compare?
:
:
: Using MX is there a relatively simple way to do a fast compare of
: two large string arrays? I need to know if an element in an array
: is contained in the another.
: Currently I'd use something like this
:
: for(o = 1, o LT arrayLen(ary1), o = o + 1) {
: for(i = 1, LT arrayLen(ary2), i = i + 1) {
: if(ary1[o] EQ ary2[i]) {
: //match...
: }
: }
: }
:
: This obviously does not scale well. Given the fact that I am not a
: CS grad (no quicksort, not ready for that :)), does Java have a good
: way to do this kind of compare, that I could use in MX, or is there
: another way?
:
: --
: jon
: mailto:jonhall@;ozline.net
:
:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Your ad could be here. Monies from ads go to support these lists and provide more
resources for the community. http://www.fusionauthority.com/ads.cfm