Note that there are some explanatory texts on larger screens.

plurals
  1. POFaster Algorithm for string comparing in c#
    text
    copied!<p>I have two sentences that needed to be compared to each-other. The final result is how much percent one sentence contains in the other, my problem is that I have 100.000 records that need to be compared with lets say another 10. That is 1.000.000 loops, which in my algorithm is very slow.</p> <p>This is the algorithm that I am using:</p> <pre><code>private double BreakStringsAndCheck(string s1, string s2) { if (s1 == null || s2 == null || s1.Length == 0 || s2.Length == 0) return (double)0; string[] firstArray = s1.Split(' '); string[] secondArray = s2.Split(' '); if (firstArray.Length &gt; secondArray.Length) { string[] tempArray = firstArray; firstArray = secondArray; secondArray = tempArray; } double value = 0; for (int i = 0; i &lt; firstArray.Length; i++) for (int j = 0; j &lt; secondArray.Length; j++) value += firstArray[i] == secondArray[j] ? (double)100 : (double)0; return findLongest ? value : value / firstArray.Length; } </code></pre> <p>It's a small method but it is not very fast. From my testing I can do 40-60 comparisons in 1 second, that is almost 5 hours for 1.000.000 loops.</p> <p>Can some one think of another method or logic that is much faster than this?</p> <p>Update:</p> <p>I will try to explain the problem with more details. I have database with more than 100.000 records, and every day I insert, and compare 10-20 new record in this database. This records are sentences from 2 to 10 words and I need to write fast method that will compare this new records with those in database, the result should be percentage of how much one sentence contains the words from the other.</p> <p>I need the records that has more than 70% word match.</p> <p>I hope that I'm clear now.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload