1 00:00:00,330 --> 00:00:02,370 Earlier we compared the source code for 2 00:00:02,370 --> 00:00:06,330 performing a union versus performing a concat and a distinct. 3 00:00:06,330 --> 00:00:09,730 Union looks over two different sequences and attempts to see if 4 00:00:09,730 --> 00:00:14,270 each element already exists before adding it to a newer numerable and 5 00:00:14,270 --> 00:00:18,340 the concat looks over two sequences mashing it into one and 6 00:00:18,340 --> 00:00:22,560 then a distinct will iterate over the result and take out the duplicates. 7 00:00:22,560 --> 00:00:26,670 I'll be using workspaces to do this exercise instead of the C#. 8 00:00:26,670 --> 00:00:27,910 You should follow along with me. 9 00:00:29,030 --> 00:00:35,020 First here in the Main method we'll need two innumerables of a bunch of integers. 10 00:00:35,020 --> 00:00:39,350 We can use that Innumerable.Range to produce them for us. 11 00:00:39,350 --> 00:00:44,234 We can jam a bunch of numbers in them so we'll have a big list Var listA = 12 00:00:44,234 --> 00:00:47,591 Enumerable.Range. 13 00:00:47,591 --> 00:00:54,680 We'll go from zero and we'll give it, I don't know, 100,000 numbers. 14 00:00:56,020 --> 00:01:04,051 All right, we'll need another list, var listB = Enumerable.Range. 15 00:01:06,000 --> 00:01:09,440 And this time, we'll start halfway, 50,000, 16 00:01:09,440 --> 00:01:14,470 but we'll also give it 100,000 numbers. 17 00:01:14,470 --> 00:01:16,890 All right, big lists. 18 00:01:16,890 --> 00:01:21,430 So there's a neat little tool here in the System.Diagnostics 19 00:01:21,430 --> 00:01:24,740 namespace of the .NET framework called Stopwatch. 20 00:01:24,740 --> 00:01:28,850 We can use it to time our queries so we can find out which one is faster. 21 00:01:28,850 --> 00:01:31,570 It's an instance class so we'll need to instantiate a first. 22 00:01:32,890 --> 00:01:39,660 Stopwatch, we'll call it stopwatch, equals new stopwatch. 23 00:01:42,640 --> 00:01:51,630 And then we'll call Stopwatch.start so that I'll start our stopwatch. 24 00:01:51,630 --> 00:01:57,570 Then we can do the unions so, if I will create another list, listC 25 00:01:58,890 --> 00:02:05,820 equals listA.Union Of listB. 26 00:02:08,670 --> 00:02:15,237 Then we need to stop the stopwatch, stopwatch.Stop. 27 00:02:15,237 --> 00:02:19,870 All right, we'll need to store the elapsed time in a new variable. 28 00:02:19,870 --> 00:02:26,543 We'll call it union ticks Equals 29 00:02:26,543 --> 00:02:33,590 stopwatch.elapsedticks. 30 00:02:33,590 --> 00:02:38,290 I'm using the elapsed ticks property of the stopwatch object because a tick is 31 00:02:38,290 --> 00:02:42,920 much smaller than a millisecond A tick is 10,000 milliseconds. 32 00:02:42,920 --> 00:02:46,190 Our queries will probably run really fast, so 33 00:02:46,190 --> 00:02:49,400 using ticks will be easier for us to digest. 34 00:02:49,400 --> 00:02:51,690 We'll need to reset the stopwatch. 35 00:02:51,690 --> 00:02:56,550 We can call stopwatch.Restart. 36 00:02:56,550 --> 00:02:57,836 stopwatch. 37 00:02:57,836 --> 00:03:03,764 Restart and that actually performs a stop and 38 00:03:03,764 --> 00:03:10,940 a start in the same things so now we can do our con-cat so 39 00:03:10,940 --> 00:03:15,932 we'll make another list called List D 40 00:03:15,932 --> 00:03:20,470 equals List A dot con-cat list B. 41 00:03:20,470 --> 00:03:24,620 And then we need to do the distinct so it's the same operation. 42 00:03:24,620 --> 00:03:26,990 Removing the duplicate, okay. 43 00:03:26,990 --> 00:03:29,345 Then we'll stop the stopwatch again. 44 00:03:32,645 --> 00:03:37,290 Then we'll create another variable to hold the amount of time that that took. 45 00:03:37,290 --> 00:03:42,281 So we'll say var ConcatTicks equals 46 00:03:42,281 --> 00:03:46,971 stopwatch.ElapsedTicks. 47 00:03:49,180 --> 00:03:52,480 Right now we've got our two variables that hold the amount of time 48 00:03:52,480 --> 00:03:54,230 each operation took. 49 00:03:54,230 --> 00:04:01,825 Let's write the values to the console, Console.rightline, 50 00:04:01,825 --> 00:04:09,640 string.Format Union, 51 00:04:09,640 --> 00:04:16,850 our first operation, took that many ticks. 52 00:04:16,850 --> 00:04:20,720 All right, then we'll pass at the union ticks variable. 53 00:04:22,770 --> 00:04:24,910 Okay, and we'll do the same thing. 54 00:04:24,910 --> 00:04:30,620 I'll do a copy Ctrl+C and then I paste with Ctrl+V. 55 00:04:30,620 --> 00:04:35,430 And here for union we'll do Concat and 56 00:04:35,430 --> 00:04:40,090 then we'll replace unionTicks with concatTicks. 57 00:04:41,830 --> 00:04:42,490 Alright. 58 00:04:42,490 --> 00:04:44,442 Well, and then because we're lazy, 59 00:04:44,442 --> 00:04:47,004 let's make the program compare the two values for 60 00:04:47,004 --> 00:04:51,103 us and tell us which one is faster so that we don't have to do the math in our head. 61 00:04:53,961 --> 00:04:55,844 So if. 62 00:04:58,212 --> 00:05:03,580 Union ticks is greater than can catch ticks. 63 00:05:04,850 --> 00:05:11,494 Then we'll write out console.writeline. 64 00:05:13,120 --> 00:05:14,860 So concat would be faster here. 65 00:05:14,860 --> 00:05:20,120 So concat is faster by so 66 00:05:20,120 --> 00:05:23,014 many ticks. 67 00:05:25,255 --> 00:05:30,059 And will pass it, the result of unionTicks because 68 00:05:30,059 --> 00:05:34,090 that would be greater minus concatTicks. 69 00:05:37,310 --> 00:05:37,900 Okay. 70 00:05:40,170 --> 00:05:44,160 Then, otherwise else if 71 00:05:45,210 --> 00:05:50,660 concatTicks is greater than unionTicks. 72 00:05:53,660 --> 00:05:57,840 Then We'll copy and paste again. 73 00:05:59,280 --> 00:06:01,250 Control C and control V. 74 00:06:02,630 --> 00:06:09,700 Union would be faster, and got a type-o there. 75 00:06:09,700 --> 00:06:14,458 Then we would Subtract 76 00:06:14,458 --> 00:06:18,720 unionTicks from concatTicks and write that to the console. 77 00:06:20,370 --> 00:06:22,230 All right, do you see any typos? 78 00:06:22,230 --> 00:06:28,620 Let's compile.mcs Program.cs. 79 00:06:28,620 --> 00:06:30,320 Okay, just some warnings. 80 00:06:30,320 --> 00:06:34,461 Then we'll run with mono programme.exe. 81 00:06:36,985 --> 00:06:42,180 All right union is faster by only 3, 000 ticks. 82 00:06:43,490 --> 00:06:47,680 We saw earlier that union iterated two times but the concat and 83 00:06:47,680 --> 00:06:50,490 distinct iterated a total of three times. 84 00:06:50,490 --> 00:06:53,610 So the more times a sequence is iterated over, 85 00:06:53,610 --> 00:06:56,120 the more time it will take to perform the operation.