Fastest Way to Compare Strings in C# .Net
What is the fastest way to compare strings in C# .Net? Everyone’s had to do it. A lot of people instinctively use the C# “==” operator. In several programming forums I’ve posted to, quite a few haven’t been aware of other built in methods or techniques that can be used, such as SequenceEqual, comparing ByteArrays, or simply using the favorite Dictionary/Hashset objects to check against the keys.
The Background:
Comparing strings is one of the basic fundamentals in any programming language. The most common syntax across multiple languages is simply the “==” operator. In C# .Net, the “==” operator isn’t always appropriate to use. Thus, Microsoft has included additional methods to compare string values. Then there are ways of comparing strings, which a lot of C# programmers have done, but may not realized. For example, checking a Dictionary object to determine if it contains a particular string as a key. That in itself is doing a string comparison.
With that said, what is the fastest way to compare strings in C# .Net? Is anyone else as curious as this Curious Consultant?
So let’s get started.
The Gist of it All:
I implemented what I think would be the most common ways of doing string comparisons. I chose the Dictionary and Hashset objects as the choice when comparing against keys because they proved to be the fastest in my test on collections for fastest string look ups (http://cc.davelozinski.com/c-sharp/fastest-collection-for-string-lookups) test.
If there’s another method people have, let me know so I can include it in the code!
Here’s the gist of the tests I included:
Code Snippet: |
|||
Case Sensitive: |
|||
== |
|
||
|
|||
|
|||
|
|||
char by char |
|
||
Byte Arrays |
|
||
Dictionary Key |
|
||
Hashset Key |
|
||
|
|||
CommonMethod |
|
||
SequenceEqual |
|
||
Case Insensitive: |
|||
== |
|
||
|
|||
|
|||
|
|||
|
|||
char by char |
|
||
Byte Arrays |
|
||
Dictionary Key |
|
||
Hashset Key |
|
||
|
|||
CommonMethod |
|
||
SequenceEqual |
|
The code is written in Visual Studio 2013 targeting .Net Framework version 4.5 x64. The source code is available at the end of this blog so you can benchmark it on your own system if you wish.
In a nutshell, the code does the following:
- Creates the storage objects, pre-sizing them where possible.
- Generates the random strings to compare of varying lengths.
- Also generates the strings to search for, purposely copying 1 of every 3 random strings to search to make sure we have at least a few matches.
- Performs the comparisons, implementing any of the various techniques in both a case and case-insensitive manner.
- Whenever a match is found, a counter is incremented. This is to ensure every technique matches the exact same amount.
The exe file was run on a Windows 7 64-bit with 16 GB memory.
The test was run for the following comparisons:
- 100 strings
- 1,000 strings
- 10,000 strings
- 100,000 strings
- 1,000,000 strings
Off To The Races!
My prediction is the Dictionary or Common Custom method will be the fastest. I’ve also heard good things about the String.Compare functions, but haven’t used them enough to form an opinion.
Let’s see what happened on my machine. I ran this test 3 times, only taking the times from the 3rd run.
All times are indicated in seconds.milliseconds format. Lower numbers indicate faster runtime performance.
Winners are highlighted in green; there are no points for second place.
Comparison Performance: |
|||||
# Strings Being Compared: |
100 |
1,000 |
10,000 |
100,000 |
1,000,000 |
——– Case Sensitive Comparisons: ——– |
|||||
== |
0 |
0 |
0 |
0.0030001 |
0.0156000 |
String.Compare |
0 |
0 |
0 |
0.0160009 |
0.0936001 |
String.CompareOrdinal |
0 |
0 |
0 |
0 |
0.0156000 |
String.Equals |
0 |
0 |
0 |
0 |
0.0156001 |
char by char |
0 |
0 |
0 |
0 |
0.0312001 |
Using Byte array |
0 |
0 |
0.0156001 |
0.0624001 |
0.4130236 |
Dictionary Keycheck |
0 |
0 |
0 |
0 |
0.1800103 |
Hashset Keycheck |
0.0030002 |
0 |
0 |
0 |
0.1860107 |
IndexOf |
0 |
0 |
0.0060004 |
0.0156000 |
0.1570090 |
Common Custom Method |
0 |
0 |
0.0020001 |
0.0070004 |
0.0624001 |
SequenceEqual Method |
0.0070004 |
0.0010001 |
0.0120007 |
0.0250015 |
0.1716003 |
——– Case Insensitive Comparisons: ——– |
|||||
== |
0 |
0 |
0 |
0.0550032 |
0.2652005 |
String.Compare |
0 |
0 |
0 |
0.0200012 |
0.0936002 |
String.Equals |
0 |
0 |
0.0010000 |
0.0150009 |
0.0468001 |
ToLower() |
0 |
0.0010000 |
0.0060004 |
0.0500028 |
0.2652005 |
ToUpper() |
0 |
0.0010001 |
0.0180010 |
0.0300017 |
0.2496004 |
char by char |
0 |
0 |
0.0060003 |
0.0156000 |
0.2028004 |
Using Byte array |
0 |
0.0010001 |
0.0070004 |
0.0312001 |
0.3890223 |
Dictionary Keycheck |
0 |
0 |
0 |
0.0156000 |
0.1810103 |
Hashset Keycheck |
0 |
0 |
0 |
0.0156000 |
0.1820104 |
IndexOf |
0 |
0 |
0 |
0.0156000 |
0.1290074 |
Common Custom Method |
0 |
0 |
0 |
0.0312000 |
0.2808005 |
SequenceEqual Method |
0 |
0 |
0 |
0.0156001 |
0.1092001 |
The Results:
Most of the board remained green up through 10,000 comparisons and didn’t register any time.
At the 100,000 and 1,000,000 marks, things started to get a bit more interesting in terms of time differences.
String.CompareOrdinal was the constant superstar. What surprised me is for the case-insensitive comparisons, String.CompareOrdinal outperformed most other methods by a whole decimal place.
For case sensitive comparisons, most programmers can probably stick with the “==” operator.
In Summary:
On my system, unless someone spots a flaw in my test code, if you are doing just a few calls or searches here and there, almost anything should suit your needs and perform fine, especially if you only have a few comparisons to do. Even though there are a few technicalities and times when it shouldn’t be used, I personally like using the “==” operator because it’s simple, straight forward, and incredibly easy to read at just a glance.
The Code:
Note: this code has been updated since the results posted above to utilize the StopWatch for time keeping. The resulting wins/losses are similar, so didn’t feel a need to update the results table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Concurrent; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; namespace TestApplication { class Program { static void Main(string[] args) { DateTime end; DateTime start = DateTime.Now; Console.WriteLine("### Overall Start Time: " + start.ToLongTimeString()); Console.WriteLine(); #region CompareTwoStrings FastestWayToCompareTwoStrings(100, true); FastestWayToCompareTwoStrings(1000, true); FastestWayToCompareTwoStrings(10000, true); FastestWayToCompareTwoStrings(100000, true); FastestWayToCompareTwoStrings(1000000, true); FastestWayToCompareTwoStrings(100, false); FastestWayToCompareTwoStrings(1000, false); FastestWayToCompareTwoStrings(10000, false); FastestWayToCompareTwoStrings(100000, false); FastestWayToCompareTwoStrings(1000000, false); #endregion end = DateTime.Now; Console.WriteLine(); Console.WriteLine("### Overall End Time: " + end.ToLongTimeString()); Console.WriteLine("### Overall Run Time: " + (end - start)); Console.WriteLine(); Console.WriteLine("Hit Enter to Exit"); Console.ReadLine(); } //#################################################### //Which way is faster to compare to string? static void FastestWayToCompareTwoStrings(int numberOfStringsToCompare, bool caseSensitive) { Stopwatch sw = new Stopwatch(); DateTime end = DateTime.Now; string[] stringsWeAreComparingAgainst = new String[numberOfStringsToCompare]; string[] stringsWeWantToSeeIfMatches = new String[numberOfStringsToCompare]; int numberOfMatches = 0; int z = 4; bool isDifferent = false; byte[] b1 = null; byte[] b2 = null; char[] c1 = null; char[] c2 = null; string[] s1 = null; string[] s2 = null; HashSet<string> hs = new HashSet<string>(); Dictionary<string, string> d = new Dictionary<string, string>(numberOfStringsToCompare); Console.WriteLine("######## " + System.Reflection.MethodBase.GetCurrentMethod().Name); Console.WriteLine("######## Number of strings to compare: " + numberOfStringsToCompare.ToString("#,##0")); Console.WriteLine("######## Case Sensitive: " + caseSensitive.ToString()); Console.WriteLine("###########################################################"); Console.WriteLine(); try { //set up the array of strings to use Console.WriteLine("###########################################################"); Console.WriteLine("Starting creating string arrays with " + numberOfStringsToCompare.ToString("#,##0") + " strings"); Console.WriteLine("###########################################################"); for (int x = 0; x < numberOfStringsToCompare; x++) { //Use the generate password function so we can easily vary the length of the strings and the chars within stringsWeAreComparingAgainst[x] = System.Web.Security.Membership.GeneratePassword(z, z % 4); if (x % 3 == 0) { //to ensure we have some matches stringsWeWantToSeeIfMatches[x] = stringsWeAreComparingAgainst[x]; } else { stringsWeWantToSeeIfMatches[x] = System.Web.Security.Membership.GeneratePassword(z, z % 4); } if (!d.ContainsKey(stringsWeAreComparingAgainst[x])) { d.Add(stringsWeAreComparingAgainst[x], string.Empty); hs.Add(stringsWeAreComparingAgainst[x]); } if (z > 25) { z = 4; } else { z += 1; } } Console.WriteLine("Completed creating string arrays with " + numberOfStringsToCompare.ToString("#,##0") + " strings"); Console.WriteLine("###########################################################"); // using the == //it's case sensitive so there will be less matches Thread.Sleep(1000); numberOfMatches = 0; if (caseSensitive) { Console.WriteLine("Starting == Case Sensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (stringsWeWantToSeeIfMatches[x] == stringsWeAreComparingAgainst[x]) numberOfMatches += 1; } sw.Stop(); } else { Console.WriteLine("Starting == Case Insensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (stringsWeWantToSeeIfMatches[x].ToLower() == stringsWeAreComparingAgainst[x].ToLower()) numberOfMatches += 1; } sw.Stop(); } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); Thread.Sleep(1000); numberOfMatches = 0; //String.Compare Console.WriteLine("Starting String.Compare: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (string.Compare(stringsWeWantToSeeIfMatches[x], stringsWeAreComparingAgainst[x], caseSensitive) == 0) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); Thread.Sleep(1000); numberOfMatches = 0; //String.CompareOrdinal Console.WriteLine("Starting String.CompareOrdinal: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (string.CompareOrdinal(stringsWeWantToSeeIfMatches[x], stringsWeAreComparingAgainst[x]) == 0) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); Thread.Sleep(1000); numberOfMatches = 0; //String.Equals //put the if statement outside the for-loops so it doesn't impact on run time if (caseSensitive) { Console.WriteLine("Starting String.Equals Case Sensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (String.Equals(stringsWeWantToSeeIfMatches[x], stringsWeAreComparingAgainst[x])) numberOfMatches += 1; } } else { Console.WriteLine("Starting String.Equals Case Insensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (String.Equals(stringsWeWantToSeeIfMatches[x], stringsWeAreComparingAgainst[x], StringComparison.OrdinalIgnoreCase)) numberOfMatches += 1; } } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); //Test using the ToUpper and ToLower methods only if we don't care about case. if (!caseSensitive) { Thread.Sleep(1000); numberOfMatches = 0; Console.WriteLine("Starting ToLower(): " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (stringsWeWantToSeeIfMatches[x].ToLower() == stringsWeAreComparingAgainst[x].ToLower()) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); Thread.Sleep(1000); numberOfMatches = 0; Console.WriteLine("Starting ToUpper(): " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (stringsWeWantToSeeIfMatches[x].ToUpper() == stringsWeAreComparingAgainst[x].ToUpper()) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } Thread.Sleep(1000); numberOfMatches = 0; //check char by char //again, put case check outside loop to not affect run time if (caseSensitive) { Console.WriteLine("Starting char by char Case Sensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { isDifferent = false; if (stringsWeAreComparingAgainst[x].Length != stringsWeWantToSeeIfMatches[x].Length) { isDifferent = true; continue; } else { for (int y = 0; y < stringsWeWantToSeeIfMatches[x].Length; y++) { if (stringsWeWantToSeeIfMatches[x][y] != stringsWeAreComparingAgainst[x][y]) { isDifferent = true; break; } } if (!isDifferent) numberOfMatches += 1; } } } else { Console.WriteLine("Starting char by char Case Insensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { isDifferent = false; if (stringsWeAreComparingAgainst[x].Length != stringsWeWantToSeeIfMatches[x].Length) { isDifferent = true; continue; } else { for (int y = 0; y < stringsWeWantToSeeIfMatches[x].Length; y++) { if (char.ToUpperInvariant(stringsWeWantToSeeIfMatches[x][y]) != char.ToUpperInvariant(stringsWeAreComparingAgainst[x][y])) { isDifferent = true; break; } } if (!isDifferent) numberOfMatches += 1; } } } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); //use byte arrays method //bytes are case sensitive so there will be less matches Thread.Sleep(1000); numberOfMatches = 0; Console.WriteLine("Starting Byte Arrays : " + DateTime.Now.ToLongTimeString()); sw.Restart(); //convert each string to a char[] and perform an intersect to see if equal for (int x = 0; x < numberOfStringsToCompare; x++) { b1 = Encoding.Unicode.GetBytes(stringsWeWantToSeeIfMatches[x]); b2 = Encoding.Unicode.GetBytes(stringsWeAreComparingAgainst[x]); if (b1.SequenceEqual(b2)) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); //use a dictionary to see if the key exists //it is case sensitive so there will be less matches Thread.Sleep(1000); numberOfMatches = 0; Console.WriteLine("Starting Dictionary : " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (d.ContainsKey(stringsWeWantToSeeIfMatches[x])) { numberOfMatches += 1; } } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); //use a hashset to see if the key exists //it is case sensitive so there will be less matches Thread.Sleep(1000); numberOfMatches = 0; Console.WriteLine("Starting Hashset : " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (hs.Contains(stringsWeWantToSeeIfMatches[x])) { numberOfMatches += 1; } } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); Thread.Sleep(1000); numberOfMatches = 0; //IndexOf method for whole length of string if (caseSensitive) { Console.WriteLine("Starting IndexOf : " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (stringsWeAreComparingAgainst[x].IndexOf(stringsWeWantToSeeIfMatches[x], 0, stringsWeWantToSeeIfMatches[x].Length) == 0) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } else { //have to do some mods to do a case insensitive comparison Console.WriteLine("Starting IndexOf : " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { if (stringsWeAreComparingAgainst[x].IndexOf(stringsWeWantToSeeIfMatches[x], 0, stringsWeWantToSeeIfMatches[x].Length, StringComparison.OrdinalIgnoreCase) == 0) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } Thread.Sleep(1000); numberOfMatches = 0; //Common method if (caseSensitive) { Console.WriteLine("Starting CommonMethod Case Sensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { numberOfMatches += ((stringsWeAreComparingAgainst[x].Length - stringsWeAreComparingAgainst[x].Replace(stringsWeWantToSeeIfMatches[x], String.Empty).Length) / stringsWeWantToSeeIfMatches[x].Length > 0 ? 1 : 0); } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } else { Console.WriteLine("Starting CommonMethod Case Insensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { numberOfMatches += ((stringsWeAreComparingAgainst[x].Length - stringsWeAreComparingAgainst[x].ToLower().Replace(stringsWeWantToSeeIfMatches[x].ToLower(), String.Empty).Length) / stringsWeWantToSeeIfMatches[x].Length > 0 ? 1 : 0); } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } Thread.Sleep(1000); numberOfMatches = 0; //SequenceEqual method if (caseSensitive) { Console.WriteLine("Starting SequenceEqual Case Sensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { c1 = stringsWeWantToSeeIfMatches[x].ToCharArray(); c2 = stringsWeAreComparingAgainst[x].ToCharArray(); if (c1.SequenceEqual(c2)) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } else { //have to do some mods to do a case insensitive comparison Console.WriteLine("Starting SequenceEqual Case Insensitive: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfStringsToCompare; x++) { s1 = new[] { stringsWeWantToSeeIfMatches[x] }; s2 = new[] { stringsWeAreComparingAgainst[x] }; if (s1.SequenceEqual(s2, StringComparer.OrdinalIgnoreCase)) numberOfMatches += 1; } sw.Stop(); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine("Number of Matches: " + numberOfMatches); Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("###########################################################"); } catch (Exception e) { Console.WriteLine("An EXCEPTION occurred as follows: " + Environment.NewLine + Environment.NewLine + e.ToString()); } finally { //Clean up if (stringsWeAreComparingAgainst != null) { Array.Clear(stringsWeAreComparingAgainst, 0, stringsWeAreComparingAgainst.Length); stringsWeAreComparingAgainst = null; } if (stringsWeWantToSeeIfMatches != null) { Array.Clear(stringsWeWantToSeeIfMatches, 0, stringsWeWantToSeeIfMatches.Length); stringsWeWantToSeeIfMatches = null; } if (b1 != null) { Array.Clear(b1, 0, b1.Length); b1 = null; } if (b2 != null) { Array.Clear(b2, 0, b2.Length); b2 = null; } if (c1 != null) { Array.Clear(c1, 0, c1.Length); c1 = null; } if (c2 != null) { Array.Clear(c2, 0, c2.Length); c2 = null; } if (s1 != null) { Array.Clear(s1, 0, s1.Length); s1 = null; } if (s2 != null) { Array.Clear(s2, 0, s2.Length); s2 = null; } hs.Clear(); hs = null; d.Clear(); d = null; GC.Collect(); } } } } |