Fastest Collection for String Lookups in C# .Net
All C# programmers have had to do this at some point in their career… look for a particular string as some sort of key or value within a data structure. But how do the multitude of data structures compare against each other for the fastest lookup of said key or value? What is the fastest collection for string lookups in C#?
The Background:
In C#, some of the most common collections for storing data are arrays, lists, dictionaries, and collections based on hashes. Of these, some allow for the storage of “keys” as strings; others only allow strings to be stored as “values”; and there are some which take the middle ground of allowing strings to be stored as both a “key” and a “value”.
This test stemmed from several projects where the code created dictionary objects or HashSets as in-memory references to see if particular records already existed with the particular string as a key.
I saw a lot of conditions similar to:
1 2 3 |
if (myDict.ContainsKey(theValue)) { … } if (myHashSet.Contains(theValue)) { … } if (theArrayList.Contains( … ) |
So that’s when this curious consultant started wondering… what’s the fastest collection for string lookups in C# .Net? Whether by key or value?
Here’s the Setup:
I came up with a list of collections I believe are commonly used, and broke them into two groups: those that allow for “strings” to be used as “keys” for lookups, and those that allow for strings to be used as “values”. The list is divided as follows:
Searching Keys:
Searching Values:
- Array
- Array custom method
- Array Binary Search
- ArrayList – contains
- ArrayList – for loop
- List – contains
- List – for loop
- List Binary Search
- SortedList
- Dictionary
- SortedDictionary
- ConcurrentDictionary
- HashTable
I wrote a C# Console application to test several methods of key and value lookups.
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 search. Each object is populated with the exact same strings.
- 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.
- Searches the objects by their key, value, or both, implementing any of the various techniques.
- Whenever a match is found, a counter is incremented. This is to ensure every technique finds the exact same amount.
The code tests both the number and length of the strings as I was curious if the length of the strings impacted the performance at all.
The exe file was run on Windows 7 64-bit with 16 GB memory.
The test was run for the following comparisons:
- Searching 100 strings for strings that are 12, 50, and 128 characters in length
- Searching 5,000 strings for strings that are 12, 50, and 128 characters in length
- Searching 25,000 strings for strings that are 12, 50, and 128 characters in length
- Searching 100,000 strings for strings that are 12, 50, and 128 characters in length
The Runs:
My prediction is the Dictionary (by key), Sorted Dictionary (by key), and HashSet would provide the fastest overall performance; when it comes to Arrays and Lists, I think it will be a toss up between the binary searches (as the amount of data gets larger) and the Array custom method.
Let’s see what happened on my machine.
All times are indicated in minutes:seconds.milliseconds format. Lower numbers indicate faster run time performance.
The winners are highlighted in green:
|
Search Performance: |
||
# to Search: |
100 |
||
String Lengths: |
12 |
50 |
128 |
——– Single Threaded “Key” Search: ——– |
|||
Dictionary (by key) |
00:00 |
00:00 |
00:00 |
Sorted Dictionary (by key) |
00:00 |
00:00 |
00:00 |
Concurrent Dictionary (by key) |
00:00 |
00:00 |
00:00 |
HashSet |
00:00 |
00:00 |
00:00 |
HashTable (by key) |
00:00 |
00:00 |
00:00 |
|
|
||
——– Single Threaded “Value” Search: ——– |
|||
Array |
00:00 |
00:00 |
00:00 |
Array custom method |
00:00 |
00:00 |
00:00 |
Array: Binary Search |
00:00 |
00:00 |
00:00 |
Array List – Contains |
00:00 |
00:00 |
00:00 |
Array List – For Loop |
00:00 |
00:00 |
00:00 |
List – Contains |
00:00 |
00:00 |
00:00 |
List – For Loop |
00:00 |
00:00 |
00:00 |
List: Binary Search |
00:00 |
00:00 |
00:00 |
Sorted List |
00:00 |
00:00 |
00:00 |
Dictionary (by value) |
00:00 |
00:00 |
00:00 |
Sorted Dictionary (by value) |
00:00.0312001 |
00:00 |
00:00 |
Concurrent Dictionary (by value) |
00:00 |
00:00 |
00:00 |
HashTable (by value) |
00:00 |
00:00 |
00:00 |
|
|
||
——– Parallel “Key” Search: ——– |
|||
Dictionary (by key) |
00:00 |
00:00 |
00:00 |
Sorted Dictionary (by key) |
00:00 |
00:00 |
00:00 |
Concurrent Dictionary (by key) |
00:00 |
00:00 |
00:00 |
HashSet |
00:00 |
00:00 |
00:00 |
HashTable (by key) |
00:00 |
00:00 |
00:00 |
|
|
||
——– Parallel “Value” Search: ——– |
|||
Array |
00:00.2652005 |
00:00 |
00:00 |
Array – Custom method |
00:00 |
00:00 |
00:00 |
Array: Binary Search |
00:00 |
00:00 |
00:00 |
Array List – Contains |
00:00 |
00:00 |
00:00 |
Array List – For Loop |
00:00 |
00:00 |
00:00 |
List – Contains |
00:00 |
00:00 |
00:00 |
List – For Loop |
00:00 |
00:00 |
00:00 |
List: Binary Search |
00:00 |
00:00 |
00:00 |
Sorted List |
00:00 |
00:00 |
00:00 |
Dictionary (by value) |
00:00 |
00:00 |
00:00 |
Sorted Dictionary (by value) |
00:00 |
00:00 |
00:00 |
Concurrent Dictionary (by value) |
00:00 |
00:00 |
00:00 |
HashTable (by value) |
00:00 |
00:00 |
00:00 |
|
SearchPerformance: |
|||
# to Search: |
5,000 |
|||
String Lengths: |
12 |
50 |
128 |
|
——– Single Threaded “Key” Search: ——– |
||||
Dictionary (by key) |
00:00 |
00:00 |
00:00 |
|
Sorted Dictionary (by key) |
00:00.0156000 |
00:00.0156000 |
00:00.0156000 |
|
Concurrent Dictionary (by key) |
00:00 |
00:00 |
00:00 |
|
HashSet |
00:00 |
00:00 |
00:00 |
|
HashTable (by key) |
00:00 |
00:00 |
00:00 |
|
|
|
|||
——– Single Threaded “Value” Search: ——– |
||||
Array |
00:01.4352025 |
00:01.4508026 |
00:01.4196025 |
|
Array custom method |
00:00.8892016 |
00:00.8580015 |
00:00.8736016 |
|
Array: Binary Search |
00:00.0156000 |
00:00.0156000 |
00:00.0156001 |
|
Array List – Contains |
00:00.2028003 |
00:00.2028004 |
00:00.2184004 |
|
Array List – For Loop |
00:00.0936002 |
00:00.1246002 |
00:00.1248002 |
|
List – Contains |
00:00.2340005 |
00:00.2652004 |
00:00.2652005 |
|
List – For Loop |
00:00.2184004 |
00:00.2184004 |
00:00.2184003 |
|
List: Binary Search |
00:00.0156000 |
00:00.0156000 |
00:00.0156000 |
|
Sorted List |
00:00.0156000 |
00:00.0156000 |
00:00.0156001 |
|
Dictionary (by value) |
00:00.2496004 |
00:00.2652004 |
00:00.2652005 |
|
Sorted Dictionary (by value) |
00:00.9672017 |
00:00.7488013 |
00:00.7332013 |
|
Concurrent Dictionary (by value) |
00:01.0140018 |
00:01.0296018 |
00:01.0140018 |
|
HashTable (by value) |
00:00.2964005 |
00:00.2808005 |
00:00.2964006 |
|
|
|
|||
——– Parallel “Key” Search: ——– |
||||
Dictionary (by key) |
00:00 |
00:00 |
00:00 |
|
Sorted Dictionary (by key) |
00:00 |
00:00 |
00:00 |
|
Concurrent Dictionary (by key) |
00:00 |
00:00 |
00:00 |
|
HashSet |
00:00 |
00:00 |
00:00 |
|
HashTable (by key) |
00:00 |
00:00 |
00:00 |
|
|
|
|||
——– Parallel “Value” Search: ——– |
||||
Array |
00:00.3900007 |
00:00.3744006 |
00:00.3900006 |
|
Array – Custom method |
00:00.2340004 |
00:00.2184004 |
00:00.2340004 |
|
Array: Binary Search |
00:00 |
00:00 |
00:00 |
|
Array List – Contains |
00:00.0312000 |
00:00.0624001 |
00:00.0780001 |
|
Array List – For Loop |
00:00.0156000 |
00:00.0468001 |
00:00.0468001 |
|
List – Contains |
00:00.0780002 |
00:00.0780001 |
00:00.0780001 |
|
List – For Loop |
00:00.0468001 |
00:00.0624001 |
00:00.0780001 |
|
List: Binary Search |
00:00 |
00:00 |
00:00 |
|
Sorted List |
00:00 |
00:00 |
00:00 |
|
Dictionary (by value) |
00:00.0936002 |
00:00.0936002 |
00:00.0936001 |
|
Sorted Dictionary (by value) |
00:00.1560002 |
00:00.3588006 |
00:00.1872003 |
|
Concurrent Dictionary (by value) |
00:01.2012021 |
00:01.2168021 |
00:01.2168021 |
|
HashTable (by value) |
00:00.0936001 |
00:00.0936002 |
00:00.0936001 |
|
SearchPerformance: |
||
# to Search: |
25,000 |
||
String Lengths: |
12 |
50 |
128 |
——– Single Threaded “Key” Search: ——– |
|||
Dictionary (by key) |
00:00 |
00:00 |
00:00.0156000 |
Sorted Dictionary (by key) |
00:00.0624001 |
00:000.624001 |
00:00.0780001 |
Concurrent Dictionary (by key) |
00:00 |
00:00 |
00:00.0156000 |
HashSet |
00:00 |
00:00 |
00:00.0156001 |
HashTable (by key) |
00:00 |
00:00 |
00:00.0156001 |
|
|
||
——– Single Threaded “Value” Search: ——– |
|||
Array |
00:36.9408649 |
00:39.0936686 |
00:42.9312754 |
Array custom method |
00:20.5452361 |
00:22.5576396 |
00:21.0600370 |
Array: Binary Search |
00:00.0780001 |
00:00.0624001 |
00:00.0624001 |
Array List – Contains |
00:04.3524076 |
00:07.9248139 |
00:07.9248139 |
Array List – For Loop |
00:02.3712042 |
00:02.3712042 |
00:02.3712042 |
List – Contains |
00:05.5068096 |
00:08.2680146 |
00:10.1244177 |
List – For Loop |
00:04.7112083 |
00:06.4896114 |
00:07.0356124 |
List: Binary Search |
00:00.0624001 |
00:00.0480002 |
00:00.0468000 |
Sorted List |
00:00.0468000 |
00:00.0468001 |
00:00.0624001 |
Dictionary (by value) |
00:05.9592105 |
00:09.5160167 |
00:10.9824193 |
Sorted Dictionary (by value) |
00:18.0492318 |
00:22.8228401 |
00:25.7868453 |
Concurrent Dictionary (by value) |
00:36.2700637 |
00:39.9984703 |
00:47.2368830 |
HashTable (by value) |
00:07.3788129 |
00:07.6440134 |
00:07.7688136 |
|
|
||
——– Parallel “Key” Search: ——– |
|||
Dictionary (by key) |
00:00 |
00:00 |
00:00 |
Sorted Dictionary (by key) |
00:00.0312000 |
00:00.0312001 |
00:00.0312000 |
Concurrent Dictionary (by key) |
00:00 |
00:00 |
00:00 |
HashSet |
00:00 |
00:00 |
00:00 |
HashTable (by key) |
00:00 |
00:00 |
00:00 |
|
|
||
——– Parallel “Value” Search: ——– |
|||
Array |
00:09.7188171 |
00:09.9060174 |
00:09.6876170 |
Array – Custom method |
00:05.1948091 |
00:05.2884093 |
00:05.3508094 |
Array: Binary Search |
00:00.0312000 |
00:00.0312000 |
00:00.0156000 |
Array List – Contains |
00:01.0296018 |
00:01.3728024 |
00:01.4976026 |
Array List – For Loop |
00:00.6864012 |
00:00.6708012 |
00:00.6552011 |
List – Contains |
00:01.4196025 |
00:01.6848029 |
00:01.4976026 |
List – For Loop |
00:00.9516016 |
00:01.2012021 |
00:00.6552011 |
List: Binary Search |
00:00.0312001 |
00:00.0312001 |
00:00.0156000 |
Sorted List |
00:00.0156000 |
00:00.0156001 |
00:00.0156000 |
Dictionary (by value) |
00:01.4508026 |
00:01.8096032 |
00:02.1216037 |
Sorted Dictionary (by value) |
00:03.7752066 |
00:04.1184073 |
00:04.2588075 |
Concurrent Dictionary (by value) |
00:44.5848783 |
00:47.0496827 |
00:51.3552902 |
HashTable (by value) |
00:01.7472030 |
00:01.7940032 |
00:01.9188034 |
|
SearchPerformance: |
||
# to Search: |
100,000 |
||
String Lengths: |
12 |
50 |
128 |
——– Single Threaded “Key” Search: ——– |
|||
Dictionary (by key) |
00:00.0312001 |
00:00.0468001 |
00:00.0468001 |
Sorted Dictionary (by key) |
00:00.2184004 |
00:00.2340004 |
00:00.2496004 |
Concurrent Dictionary (by key) |
00:00.0312000 |
00:00.0312001 |
00:00.0468001 |
HashSet |
00:00.1560000 |
00:00.0312001 |
00:00.0468000 |
HashTable (by key) |
00:00.0312000 |
00:00.0156000 |
00:00.0468001 |
|
|
||
——– Single Threaded “Value” Search: ——– |
|||
Array |
11:36.0108225 |
13:46.5674518 |
17:01.7081945 |
Array custom method |
08:21.6500811 |
10:02.8786589 |
11:23.7336009 |
Array: Binary Search |
00:00.1872003 |
00:00.2184004 |
00:00.2340004 |
Array List – Contains |
02:59.1447150 |
03:46.2315974 |
04:16.9324513 |
Array List – For Loop |
00:37.5024659 |
00:37.3932657 |
00:37.3308655 |
List – Contains |
03:28.6347665 |
04:27.4780698 |
05:02.8277319 |
List – For Loop |
02:57.1071110 |
03:37.6359822 |
05:01.3457293 |
List: Binary Search |
00:00.1872003 |
00:00.2028004 |
00:00.2340004 |
Sorted List |
00:00.1716003 |
00:00.1872003 |
00:00.1716003 |
Dictionary (by value) |
03:34.7031772 |
04:34.3576819 |
05:11.0489463 |
Sorted Dictionary (by value) |
18:41.6731701 |
20:04.8681162 |
20:40.2333784 |
Concurrent Dictionary (by value) |
21:37.0018781 |
22:53.0208115 |
24:20.6929656 |
HashTable (by value) |
05:59.2842311 |
06:07.3494452 |
06:13.7766565 |
|
|
||
——– Parallel “Key” Search: ——– |
|||
Dictionary (by key) |
00:00 |
00:00 |
00:00.0156001 |
Sorted Dictionary (by key) |
00:00.0780001 |
00:00.0780001 |
00:00.0780002 |
Concurrent Dictionary (by key) |
00:00 |
00:00.0156001 |
00:00.0156000 |
HashSet |
00:00 |
00:00 |
00:00.0156000 |
HashTable (by key) |
00:00 |
00:00 |
00:00.0156000 |
|
|
||
——– Parallel “Value” Search: ——– |
|||
Array |
03:14.2047411 |
03:32.1291726 |
03:32.5035732 |
Array – Custom method |
01:48.4669905 |
02:00.3542114 |
01:49.4809923 |
Array: Binary Search |
00:00.0780001 |
00:00.0780001 |
00:00.0780001 |
Array List – Contains |
00:46.6596819 |
00:55.8168981 |
00:57.7201014 |
Array List – For Loop |
00:11.5752203 |
00:11.5284203 |
00:11.3412199 |
List – Contains |
00:51.8232910 |
01:06.3625165 |
01:14.2405304 |
List – For Loop |
00:43.5396765 |
00:52.3692920 |
00:47.2056830 |
List: Binary Search |
00:00.0624001 |
00:00.0780001 |
00:00.0780001 |
Sorted List |
00:00.0780001 |
00:00.0624001 |
00:00.0624001 |
Dictionary (by value) |
00:56.6748995 |
01:17.2045356 |
01:23.1793460 |
Sorted Dictionary (by value) |
02:53.8779054 |
03:26.0919620 |
03:32.7843738 |
Concurrent Dictionary (by value) |
13:52.1210615 |
14:07.0034877 |
15:20.4172166 |
HashTable (by value) |
01:17.7817367 |
01:20.4337412 |
01:22.4773449 |
The Results:
When it came to searching 100 strings (either as keys or values), most of the techniques registered 00:00 time.
However, once I started increasing the number of strings to search, the results became a lot more interesting.
For starters, the SortedDictionary performed miserably compared to the other collections that allow for key lookups; even worse when searching its values. All the other collections that allowed for key lookups performed admirably.
The collections where we could only search for strings as part of the “value” had wide and varied performances. The top 3 performers were the SortedList, and doing binary searches on both a regular List and an array.
None of the collection types that implement the use of a “key” performed well when searching their “values”.
Similarly, anything that implemented LINQ using Contains was slow. (No surprises there! There should be a setting in Visual Studio to banish LINQ when needing to develop high speed code.)
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.
However, if you have a “need for speed” or are doing a heavy amount of query lookups, then if you need to search for a collection for a string:
- in the “key”, A Dictionary, ConcurrentDictionary, or Hashset are your best bets.
- In the “value”, you should use a SortedList, or convert to an array/normal List and do a binary search.
By all means, do not use LINQ. The performance difference can be summed up as follows: LINQ is like trying to melt butter with a match; whereas the top performers are akin to melting butter with a blowtorch.
The Code:
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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
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 TestFastestStructureForStringLookup //Fastest string lookup //TestFastestStructureForStringLookup(1, 12); TestFastestStructureForStringLookup(100, 12); TestFastestStructureForStringLookup(100, 50); TestFastestStructureForStringLookup(100, 128); TestFastestStructureForStringLookup(5000, 12); TestFastestStructureForStringLookup(5000, 50); TestFastestStructureForStringLookup(5000, 128); TestFastestStructureForStringLookup(25000, 12); TestFastestStructureForStringLookup(25000, 50); TestFastestStructureForStringLookup(25000, 128); TestFastestStructureForStringLookup(100000, 12); TestFastestStructureForStringLookup(100000, 50); TestFastestStructureForStringLookup(100000, 128); #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(); } //############################################################### //what is the fastest structure to lookup a string? //hashset.contains //dictionary key //concurrent dictionary key //sorted dictionary //list.containsvalue //array static void TestFastestStructureForStringLookup(int NumberOfStringsToGenerate, int LengthOfStrings) { Console.WriteLine("######## " + System.Reflection.MethodBase.GetCurrentMethod().Name); Console.WriteLine("Number of Random Strings that will be generated: " + NumberOfStringsToGenerate.ToString("#,##0")); Console.WriteLine("Length of Random Strings that will be generated: " + LengthOfStrings.ToString("#,##0")); Console.WriteLine(); object lockObject = new object(); int total = 0; DateTime end = DateTime.Now; DateTime start = DateTime.Now; string temp_str = String.Empty; string[] a = new string[NumberOfStringsToGenerate]; string[] a_bs = new string[NumberOfStringsToGenerate]; //for a binary search List l = new List(NumberOfStringsToGenerate); List l_bs = new List(NumberOfStringsToGenerate); //for binary search SortedList sl = new SortedList(NumberOfStringsToGenerate); ArrayList al = new ArrayList(NumberOfStringsToGenerate); Dictionary<string, string> d = new Dictionary<string, string>(NumberOfStringsToGenerate); ConcurrentDictionary<string, string> cd = new ConcurrentDictionary<string, string>(Environment.ProcessorCount, NumberOfStringsToGenerate); SortedDictionary<string, string> sd = new SortedDictionary<string, string>(); HashSet hs = new HashSet(); Hashtable ht = new Hashtable(NumberOfStringsToGenerate); //the strings to look up string[] ss = new string[NumberOfStringsToGenerate]; //Generate the string arrays that all the structures will read from. //This is to make sure every structure uses the same set of strings during the run. //strings to be searched. Completely random. Using generate password method to come up with all sorts of mixtures. Console.WriteLine("Generating strings to look up."); for (int x = 0; x < NumberOfStringsToGenerate; x++) { ss[x] = System.Web.Security.Membership.GeneratePassword(LengthOfStrings, x % 5); if (x % 3 == 0) temp_str = ss[x]; //to ensure there's always at least a few matches else temp_str = System.Web.Security.Membership.GeneratePassword(LengthOfStrings, x % 5); //so all the collections have the exact same strings. a[x] = temp_str; a_bs[x] = temp_str; al.Add(temp_str); l.Add(temp_str); l_bs.Add(temp_str); sl.Add(temp_str, temp_str); if (!d.ContainsKey(temp_str)) { d.Add(temp_str, temp_str); sd.Add(temp_str, temp_str); cd[temp_str] = temp_str; } hs.Add(temp_str); ht.Add(temp_str, temp_str); } Array.Sort(a_bs); //presort the binarysearch array as otherwise the results may be incorrect. l_bs.Sort(); //presort Console.WriteLine("###########################################################"); Console.WriteLine(); total = 0; Console.WriteLine("Starting searching an array... " + start.ToLongTimeString()); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { for (int y = 0; y < a.Length; y++) { if (a[y].Contains(ss[x])) { //found it. total += 1; break; } } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching an array using the custom method... " + start.ToLongTimeString()); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { for (int y = 0; y < a.Length; y++) { if ((ss[x].Length - ss[x].Replace(a[y], String.Empty).Length) / a[y].Length > 0) { //found it. total += 1; break; } } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting binary searching an array... " + start.ToLongTimeString()); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { int y = Array.BinarySearch(a_bs, ss[x]); if (y >= 0) total += 1; } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching an ArrayList using contains... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (al.Contains(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching an ArrayList using a for loop... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { for (int y = 0; y < al.Count; y++) { if (al[y] == ss[x]) { //found it. total += 1; break; } } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a List using Contains... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (l.Contains(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a List using a for loop... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { for (int y = 0; y < l.Count; y++) { if (l[y] == ss[x]) { //found it. total += 1; break; } } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting binary searching a List... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { int y = l_bs.BinarySearch(ss[x]); if (y >= 0) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a SortedList... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (sl.ContainsKey(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a Dictionary Key... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (d.ContainsKey(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a Dictionary Value... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (d.ContainsValue(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a SortedDictionary Key... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (sd.ContainsKey(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a SortedDictionary Value... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (sd.ContainsValue(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a ConcurrentDictionary Key... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (cd.ContainsKey(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a ConcurrentDictionary Value... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (cd.Values.Any(z => z == ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a HashSet... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (hs.Contains(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a HashTable Key... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (ht.ContainsKey(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a HashTable Value... "); start = DateTime.Now; for (int x = 0; x < ss.Length; x++) { if (ht.ContainsValue(ss[x])) { //found it. total += 1; } } end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine("#########Starting the parallel implementations#############"); Console.WriteLine("############################################################"); total = 0; Console.WriteLine("Starting parallel searching an array... " + start.ToLongTimeString()); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { for (int y = 0; y < a.Length; y++) { if (a[y].Contains(ss[x])) { //found it. subtotal += 1; break; } } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching an array using custom method... " + start.ToLongTimeString()); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { for (int y = 0; y < a.Length; y++) { if ((ss[x].Length - ss[x].Replace(a[y], String.Empty).Length) / a[y].Length > 0) { //found it. subtotal += 1; break; } } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel binary searching an array... " + start.ToLongTimeString()); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { int y = Array.BinarySearch(a_bs,ss[x]); if (y >= 0) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching an ArrayList using contains... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (al.Contains(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching an ArrayList using a for loop... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { for (int y = 0; y < al.Count; y++) { if (al[y]== ss[x]) { //found it. subtotal += 1; break; } } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a List using contains... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (l.Contains(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a List using a for loop... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { for (int y = 0; y < l.Count; y++) { if (l[y] == ss[x]) { //found it. total += 1; break; } } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel binary searching a List... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { int y = l_bs.BinarySearch(ss[x]); if (y >= 0) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a SortedList... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (sl.ContainsKey(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a Dictionary Key... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (d.ContainsKey(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a Dictionary Value... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (d.ContainsValue(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a SortedDictionary Key... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (sd.ContainsKey(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a SortedDictionary Value... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (sd.ContainsValue(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching ConcurrentDictionary key... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (cd.ContainsKey(ss[x])) subtotal += 1; return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching ConcurrentDictionary value... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (cd.Values.Any(z => z == ss[x])) subtotal += 1; return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting searching a HashSet... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (hs.Contains(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a HashTable Key... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (ht.ContainsKey(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Thread.Sleep(1000); //Sleep to give the system time to recover for next run total = 0; Console.WriteLine("Starting parallel searching a HashTable Value... "); start = DateTime.Now; Parallel.For(0, ss.Length, () => 0, (x, loopState, subtotal) => { if (ht.ContainsValue(ss[x])) { //found it. subtotal += 1; } return subtotal; }, (s) => { lock (lockObject) { total += s; } } ); end = DateTime.Now; Console.WriteLine("Finished at: " + end.ToLongTimeString()); Console.WriteLine("Time: " + (end - start)); Console.WriteLine("Total finds: " + total + Environment.NewLine); Console.WriteLine(); Console.WriteLine("###########################################################"); Console.WriteLine(); Array.Clear(ss, 0, ss.Length); ss = null; Array.Clear(a, 0, a.Length); a = null; al.Clear(); al = null; l.Clear(); l = null; l_bs.Clear(); l_bs = null; sl.Clear(); sl = null; d.Clear(); d = null; sd.Clear(); sd = null; cd.Clear(); cd = null; hs.Clear(); hs = null; ht.Clear(); ht = null; GC.Collect(); } //TestFastestStructureForStringLookup } } |