This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's pit the Union and Concat/Distinct operators we saw earlier and find out which is faster!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Earlier we compared the source code for
0:00
performing a union versus
performing a concat and a distinct.
0:02
Union looks over two different
sequences and attempts to see if
0:06
each element already exists before
adding it to a newer numerable and
0:09
the concat looks over two
sequences mashing it into one and
0:14
then a distinct will iterate over
the result and take out the duplicates.
0:18
I'll be using workspaces to do
this exercise instead of the C#.
0:22
You should follow along with me.
0:26
First here in the Main method we'll need
two innumerables of a bunch of integers.
0:29
We can use that Innumerable.Range
to produce them for us.
0:35
We can jam a bunch of numbers in them so
we'll have a big list Var listA =
0:39
Enumerable.Range.
0:44
We'll go from zero and we'll give it,
I don't know, 100,000 numbers.
0:47
All right, we'll need another list,
var listB = Enumerable.Range.
0:56
And this time,
we'll start halfway, 50,000,
1:06
but we'll also give it 100,000 numbers.
1:09
All right, big lists.
1:14
So there's a neat little tool
here in the System.Diagnostics
1:16
namespace of the .NET
framework called Stopwatch.
1:21
We can use it to time our queries so
we can find out which one is faster.
1:24
It's an instance class so
we'll need to instantiate a first.
1:28
Stopwatch, we'll call it stopwatch,
equals new stopwatch.
1:32
And then we'll call Stopwatch.start so
that I'll start our stopwatch.
1:42
Then we can do the unions so,
if I will create another list, listC
1:51
equals listA.Union Of listB.
1:58
Then we need to stop the stopwatch,
stopwatch.Stop.
2:08
All right, we'll need to store
the elapsed time in a new variable.
2:15
We'll call it union ticks Equals
2:19
stopwatch.elapsedticks.
2:26
I'm using the elapsed ticks property of
the stopwatch object because a tick is
2:33
much smaller than a millisecond
A tick is 10,000 milliseconds.
2:38
Our queries will probably run really fast,
so
2:42
using ticks will be easier for
us to digest.
2:46
We'll need to reset the stopwatch.
2:49
We can call stopwatch.Restart.
2:51
stopwatch.
2:56
Restart and
that actually performs a stop and
2:57
a start in the same things so
now we can do our con-cat so
3:03
we'll make another list called List D
3:10
equals List A dot con-cat list B.
3:15
And then we need to do the distinct so
it's the same operation.
3:20
Removing the duplicate, okay.
3:24
Then we'll stop the stopwatch again.
3:26
Then we'll create another variable to
hold the amount of time that that took.
3:32
So we'll say var ConcatTicks equals
3:37
stopwatch.ElapsedTicks.
3:42
Right now we've got our two variables
that hold the amount of time
3:49
each operation took.
3:52
Let's write the values to the console,
Console.rightline,
3:54
string.Format Union,
4:01
our first operation, took that many ticks.
4:09
All right, then we'll pass
at the union ticks variable.
4:16
Okay, and we'll do the same thing.
4:22
I'll do a copy Ctrl+C and
then I paste with Ctrl+V.
4:24
And here for union we'll do Concat and
4:30
then we'll replace
unionTicks with concatTicks.
4:35
Alright.
4:41
Well, and then because we're lazy,
4:42
let's make the program
compare the two values for
4:44
us and tell us which one is faster so that
we don't have to do the math in our head.
4:47
So if.
4:53
Union ticks is greater
than can catch ticks.
4:58
Then we'll write out console.writeline.
5:04
So concat would be faster here.
5:13
So concat is faster by so
5:14
many ticks.
5:20
And will pass it,
the result of unionTicks because
5:25
that would be greater minus concatTicks.
5:30
Okay.
5:37
Then, otherwise else if
5:40
concatTicks is greater than unionTicks.
5:45
Then We'll copy and paste again.
5:53
Control C and control V.
5:59
Union would be faster,
and got a type-o there.
6:02
Then we would Subtract
6:09
unionTicks from concatTicks and
write that to the console.
6:14
All right, do you see any typos?
6:20
Let's compile.mcs Program.cs.
6:22
Okay, just some warnings.
6:28
Then we'll run with mono programme.exe.
6:30
All right union is faster by only 3,
000 ticks.
6:36
We saw earlier that union iterated
two times but the concat and
6:43
distinct iterated a total of three times.
6:47
So the more times a sequence
is iterated over,
6:50
the more time it will take
to perform the operation.
6:53
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up