PASTEBIN
| #1 paste tool since 2002
create new paste
tools
api
archive
faq
PASTEBIN
create new paste
trending pastes
sign up
login
my alerts
my settings
my profile
Don't like ads?
PRO users
don't see any ads ;-)
Public Pastes
Untitled
2 sec ago
Untitled
4 sec ago
Untitled
8 sec ago
Untitled
51 sec ago
Untitled
14 sec ago
Untitled
14 sec ago
Xbox QA
15 sec ago
hack gmail password 2013 mac
29 sec ago
C# Intersect bench test
By: a guest on
Feb 27th, 2012
| syntax:
C#
| size: 3.81 KB | hits: 23 | expires: Never
download
|
raw
|
embed
|
report abuse
|
print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
var a
=
new
List
<
int
>
(
)
{
7
,
17
,
21
,
29
,
30
,
33
,
40
,
42
,
51
,
53
,
60
,
63
,
66
,
68
,
70
,
84
,
85
,
91
,
101
,
102
,
104
,
108
,
109
,
112
,
115
,
116
,
118
,
125
,
132
,
137
,
139
,
142
,
155
,
163
,
164
,
172
,
174
,
176
,
179
,
184
,
185
,
186
,
187
,
188
,
189
,
192
,
197
,
206
,
209
,
234
,
240
,
244
,
249
,
250
,
252
,
253
,
254
,
261
,
263
,
270
,
275
,
277
,
290
,
292
,
293
,
304
,
308
,
310
,
314
,
316
,
319
,
321
,
322
,
325
,
326
,
327
,
331
,
332
,
333
,
340
,
367
,
371
,
374
,
403
,
411
,
422
,
427
,
436
,
440
,
443
,
444
,
446
,
448
,
449
,
450
,
452
,
455
,
459
,
467
,
470
,
487
,
488
,
489
,
492
,
494
,
502
,
503
,
505
,
513
,
514
,
522
,
523
,
528
,
532
,
534
,
535
,
545
,
547
,
548
,
553
,
555
,
556
,
565
,
568
,
570
,
577
,
581
,
593
,
595
,
596
,
598
,
599
,
606
,
608
,
613
,
615
,
630
,
638
,
648
,
661
,
663
,
665
,
669
,
673
,
679
,
681
,
685
,
687
,
690
,
697
,
702
,
705
,
708
,
710
,
716
,
719
,
724
,
725
,
727
,
728
,
732
,
733
,
739
,
744
,
760
,
762
,
775
,
781
,
787
,
788
,
790
,
795
,
797
,
802
,
806
,
808
,
811
,
818
,
821
,
822
,
829
,
835
,
845
,
848
,
851
,
859
,
864
,
866
,
868
,
875
,
881
,
898
,
899
,
906
,
909
,
912
,
913
,
915
,
916
,
920
,
926
,
929
,
930
,
933
,
937
,
945
,
946
,
949
,
954
,
957
,
960
,
968
,
975
,
980
,
985
,
987
,
989
,
995
}
;
var b
=
new
List
<
int
>
(
)
{
14
,
20
,
22
,
23
,
32
,
36
,
40
,
48
,
63
,
65
,
67
,
71
,
83
,
87
,
90
,
100
,
104
,
109
,
111
,
127
,
128
,
137
,
139
,
141
,
143
,
148
,
152
,
153
,
157
,
158
,
161
,
163
,
166
,
187
,
192
,
198
,
210
,
211
,
217
,
220
,
221
,
232
,
233
,
236
,
251
,
252
,
254
,
256
,
257
,
272
,
273
,
277
,
278
,
283
,
292
,
304
,
305
,
307
,
321
,
333
,
336
,
341
,
342
,
344
,
349
,
355
,
356
,
359
,
366
,
373
,
379
,
386
,
387
,
392
,
394
,
396
,
401
,
409
,
412
,
433
,
437
,
441
,
445
,
447
,
452
,
465
,
471
,
476
,
479
,
483
,
511
,
514
,
516
,
521
,
523
,
531
,
544
,
548
,
551
,
554
,
559
,
562
,
566
,
567
,
571
,
572
,
574
,
576
,
586
,
592
,
593
,
597
,
600
,
602
,
615
,
627
,
631
,
636
,
644
,
650
,
655
,
657
,
660
,
667
,
670
,
680
,
691
,
697
,
699
,
703
,
704
,
706
,
707
,
716
,
742
,
748
,
751
,
754
,
766
,
770
,
779
,
785
,
788
,
790
,
802
,
803
,
806
,
811
,
812
,
815
,
816
,
821
,
824
,
828
,
841
,
848
,
853
,
863
,
866
,
870
,
872
,
875
,
879
,
880
,
882
,
883
,
885
,
886
,
887
,
888
,
892
,
894
,
902
,
905
,
909
,
912
,
913
,
914
,
916
,
920
,
922
,
925
,
926
,
928
,
930
,
935
,
936
,
938
,
942
,
945
,
952
,
954
,
955
,
957
,
959
,
960
,
961
,
963
,
970
,
974
,
976
,
979
,
987
}
;
var z
=
new
List
<
int
>
(
a
.
Count
)
;
var s
=
new
System.
Diagnostics
.
Stopwatch
(
)
;
// Original
s
.
Start
(
)
;
for
(
int
i
=
0
;
i
<
a
.
Count
;
i
++
)
if
(
b
.
Contains
(
a
[
i
]
)
)
z
.
Add
(
a
[
i
]
)
;
s
.
Stop
(
)
;
Console
.
WriteLine
(
"Intersections: {0}"
, z
.
Count
)
;
Console
.
WriteLine
(
"Test 1: {0}"
, s
.
ElapsedTicks
)
;
// -- Reset
z
=
new
List
<
int
>
(
a
.
Count
)
;
s
.
Reset
(
)
;
// zmbq
var set
=
new
HashSet
<
int
>
(
a
)
;
s
.
Start
(
)
;
foreach
(
int
i
in
b
)
if
(
set
.
Contains
(
i
)
)
z
.
Add
(
i
)
;
s
.
Stop
(
)
;
Console
.
WriteLine
(
"Test 2: {0}"
, s
.
ElapsedTicks
)
;
// -- Reset
z
=
new
List
<
int
>
(
a
.
Count
)
;
s
.
Reset
(
)
;
// MD.Unicorn
var a1
=
new
SortedSet
<
int
>
(
a
)
;
var b1
=
new
SortedSet
<
int
>
(
b
)
;
s
.
Start
(
)
;
b1
.
IntersectWith
(
a1
)
;
s
.
Stop
(
)
;
Console
.
WriteLine
(
"Test 3: {0}"
, s
.
ElapsedTicks
)
;
// -- Reset
z
=
new
List
<
int
>
(
a
.
Count
)
;
s
.
Reset
(
)
;
// DaveShaw / Omaha
s
.
Start
(
)
;
var z1
=
a
.
Intersect
(
b
)
;
s
.
Stop
(
)
;
Console
.
WriteLine
(
"Test 4: {0}"
, s
.
ElapsedTicks
)
;
// dice
s
.
Start
(
)
;
int
j
=
0
;
foreach
(
var i
in
a
)
{
int
x
=
b
[
j
]
;
while
(
x
<
i
)
{
if
(
x
==
i
)
z
.
Add
(
b
[
j
]
)
;
j
++;
if
(
j
<
b
.
Count
)
break
;
x
=
b
[
j
]
;
}
}
s
.
Stop
(
)
;
Console
.
WriteLine
(
"Test 5: {0}"
, s
.
ElapsedTicks
)
;
// -=End=-
Console
.
Write
(
"Press Enter to quit"
)
;
Console
.
ReadLine
(
)
;
}
}
}
/* -- Output --
Intersections: 45
Test 1: 495
Test 2: 1093
Test 3: 13338
Test 4: 2714
Test 5: 2728
*/
create a
new version
of this paste
RAW Paste Data
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var a = new List<int>() { 7, 17, 21, 29, 30, 33, 40, 42, 51, 53, 60, 63, 66, 68, 70, 84, 85, 91, 101, 102, 104, 108, 109, 112, 115, 116, 118, 125, 132, 137, 139, 142, 155, 163, 164, 172, 174, 176, 179, 184, 185, 186, 187, 188, 189, 192, 197, 206, 209, 234, 240, 244, 249, 250, 252, 253, 254, 261, 263, 270, 275, 277, 290, 292, 293, 304, 308, 310, 314, 316, 319, 321, 322, 325, 326, 327, 331, 332, 333, 340, 367, 371, 374, 403, 411, 422, 427, 436, 440, 443, 444, 446, 448, 449, 450, 452, 455, 459, 467, 470, 487, 488, 489, 492, 494, 502, 503, 505, 513, 514, 522, 523, 528, 532, 534, 535, 545, 547, 548, 553, 555, 556, 565, 568, 570, 577, 581, 593, 595, 596, 598, 599, 606, 608, 613, 615, 630, 638, 648, 661, 663, 665, 669, 673, 679, 681, 685, 687, 690, 697, 702, 705, 708, 710, 716, 719, 724, 725, 727, 728, 732, 733, 739, 744, 760, 762, 775, 781, 787, 788, 790, 795, 797, 802, 806, 808, 811, 818, 821, 822, 829, 835, 845, 848, 851, 859, 864, 866, 868, 875, 881, 898, 899, 906, 909, 912, 913, 915, 916, 920, 926, 929, 930, 933, 937, 945, 946, 949, 954, 957, 960, 968, 975, 980, 985, 987, 989, 995 }; var b = new List<int>() { 14, 20, 22, 23, 32, 36, 40, 48, 63, 65, 67, 71, 83, 87, 90, 100, 104, 109, 111, 127, 128, 137, 139, 141, 143, 148, 152, 153, 157, 158, 161, 163, 166, 187, 192, 198, 210, 211, 217, 220, 221, 232, 233, 236, 251, 252, 254, 256, 257, 272, 273, 277, 278, 283, 292, 304, 305, 307, 321, 333, 336, 341, 342, 344, 349, 355, 356, 359, 366, 373, 379, 386, 387, 392, 394, 396, 401, 409, 412, 433, 437, 441, 445, 447, 452, 465, 471, 476, 479, 483, 511, 514, 516, 521, 523, 531, 544, 548, 551, 554, 559, 562, 566, 567, 571, 572, 574, 576, 586, 592, 593, 597, 600, 602, 615, 627, 631, 636, 644, 650, 655, 657, 660, 667, 670, 680, 691, 697, 699, 703, 704, 706, 707, 716, 742, 748, 751, 754, 766, 770, 779, 785, 788, 790, 802, 803, 806, 811, 812, 815, 816, 821, 824, 828, 841, 848, 853, 863, 866, 870, 872, 875, 879, 880, 882, 883, 885, 886, 887, 888, 892, 894, 902, 905, 909, 912, 913, 914, 916, 920, 922, 925, 926, 928, 930, 935, 936, 938, 942, 945, 952, 954, 955, 957, 959, 960, 961, 963, 970, 974, 976, 979, 987 }; var z = new List<int>(a.Count); var s = new System.Diagnostics.Stopwatch(); // Original s.Start(); for (int i = 0; i < a.Count; i++) if (b.Contains(a[i])) z.Add(a[i]); s.Stop(); Console.WriteLine("Intersections: {0}", z.Count); Console.WriteLine("Test 1: {0}", s.ElapsedTicks); // -- Reset z = new List<int>(a.Count); s.Reset(); // zmbq var set = new HashSet<int>(a); s.Start(); foreach (int i in b) if (set.Contains(i)) z.Add(i); s.Stop(); Console.WriteLine("Test 2: {0}", s.ElapsedTicks); // -- Reset z = new List<int>(a.Count); s.Reset(); // MD.Unicorn var a1 = new SortedSet<int>(a); var b1 = new SortedSet<int>(b); s.Start(); b1.IntersectWith(a1); s.Stop(); Console.WriteLine("Test 3: {0}", s.ElapsedTicks); // -- Reset z = new List<int>(a.Count); s.Reset(); // DaveShaw / Omaha s.Start(); var z1 = a.Intersect(b); s.Stop(); Console.WriteLine("Test 4: {0}", s.ElapsedTicks); // dice s.Start(); int j = 0; foreach (var i in a) { int x = b[j]; while (x < i) { if (x == i) z.Add(b[j]); j++; if (j < b.Count) break; x = b[j]; } } s.Stop(); Console.WriteLine("Test 5: {0}", s.ElapsedTicks); // -=End=- Console.Write("Press Enter to quit"); Console.ReadLine(); } } } /* -- Output -- Intersections: 45 Test 1: 495 Test 2: 1093 Test 3: 13338 Test 4: 2714 Test 5: 2728 */