views:

601

answers:

5

Is there a fastest way to compare two strings (using the space for a wildcard) than this function?

public static bool CustomCompare(this string word, string mask)
{

    for (int index = 0; index < mask.Length; index++)
    {
        if (mask[index] != ' ') && (mask[index]!= word[index]))
        {
            return false;
        }
    }
    return true;
}

Example: "S ntnce" comparing with "Sentence" will return true.

+1  A: 

If you used . instead of , you could do a simple regex match.

Anon.
How would that be faster?
dionadar
A regular expression would most likely be slower.
Andrew Hare
The regex engine is likely to be slightly more optimized than this. It's also less code you're writing yourself, which is the real plus. Though as with any "optimization", you need to benchmark it - if it's not worth benchmarking, the performance difference doesn't matter.
Anon.
More optimized how? Regular expressions aren't magic even if they do look like some magic incantation. I really doubt the more general purpose regex code can do better than the simple loop above.
Kevin Gale
+2  A: 

That looks like a pretty good implementation - I don't think you will get much faster than that.

Have you profiled this code and found it to be a bottleneck in your application? I think this should be fine for most purposes.

Andrew Hare
Thanks for you answer.
Gregoire
+2  A: 

If mask.length is less than word.length, this function will stop comparing at the end of mask. A word/mask length compare in the beginning would prevent that, also it would quick-eliminate some obvious mismatches.

Rob Elliott
Slightly more importantly, the current implementation will throw an exception if the mask is longer than the word.
Anon.
Thanks for the suggestion. In fact I always compare strings of same length, but I will add the check, thanks.
Gregoire
+1  A: 

The loop is pretty simple and I'm not sure you can do much better. You might be able to micro optimize the order of the expression in the if statement. For example due to short circuiting of the && it might be faster to order the if statement this way

 if (mask[index]!= word[index])) && (mask[index] != ' ')

Assuming that matching characters is more common that matching the wildcard. Of course this is just theory I wouldn't believe it made a difference without benchmarking it.

And as others have pointed out the routine fails if the mask and string are not the same length.

Kevin Gale
Thanks for you answer. Currently I have the same percentage of chance to have a wildcard or a character.
Gregoire
A: 

I think you're doing a little injustice by not giving a little bit of context to your code. Sure, if you want to search only one string of characters of the same length as your pattern, then yes this is fine.

However, if you are using this as the heart of a pattern matcher where there are several other patterns you will be looking for, this is a poor method. There are other known methods, the best of which depends on your exact application. The phrase "inexact pattern matching" is the phrase you are concerned with.

San Jacinto
Thanks for the tip. As you ask for the context, I try to generate dense crosswords (less than 8% of black cases), and I search for fast way to apply constraint in getting all the words (with a specific length) that matches a pattern in a list or more or less 300000 words.
Gregoire