Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Build a Simple Benchmarker6:31 with Jason Seifer
One way that blocks are really useful is performing functionality or running code before and after the block is executed. This turns out to be really great for writing something like a benchmarker. In this video, we'll practice writing a very simple benchmarking class.
One way that blocks are really useful is performing functionality or 0:00 running code before and after a block is executed. 0:04 This turns out to be really great for writing something like a benchmarker. 0:09 We're going to write a simple one now, and here's how we're gonna do it. 0:13 First, we'll create a class for our benchmarker. 0:17 The class will have one method called run. 0:21 The run method will take a block. 0:24 We'll take the current time before the block of code is run and 0:27 store it in a variable. 0:30 Then we'll run the block, and 0:32 store the current time after the block of code is executed. 0:34 From there, we only need to subtract the end time from the start time and 0:38 print the result. 0:42 Go ahead and give it a try now. 0:45 And then come back and take a look to see how I did. 0:46 And here's a hint. 0:49 You can use the method time.now to get the current time. 0:51 Okay, so let's go ahead and build our SimpleBenchmarker class. 0:56 Now we're going to use a new part of the standard library that we haven't used 0:59 before, called the Time class. 1:04 And Time is pretty interesting. 1:07 Now, that's not a metaphor, I mean the actual Time class. 1:10 We're only really interested in one particular method from the Time class. 1:13 And let's go ahead and scroll down here. 1:20 We're looking at the now method, 1:23 which creates a new Time object for the current time. 1:27 Let's go ahead and see how that works in irb, before we start coding our class. 1:32 And just see how that works real quick. 1:38 We call Time.now, and it returns the current time. 1:40 So this is something that we're going to use in our SimpleBenchmarker class. 1:48 So we're going to create a new file and call it simple_benchmarker.rb. 1:52 And that opens it up for us. 1:57 So now we can create our SimpleBenchmarker class. 2:01 And we're going to create a method called run. 2:09 And that's gonna take a block. 2:16 And what it's gonna do is run this block of code, and 2:17 then print out the time it took to run that code. 2:22 So we can do that by saying yield or block.call. 2:26 We'll just use block.call here. 2:29 No real reason behind that, just something that we're gonna do. 2:32 And let's go ahead and get the start_time before we call the block, and 2:37 the end_time after the block is called. 2:43 So once we do that, we have the end_time and the start_time. 2:49 All we need to do is subtract the two, and we'll know the elapsed time. 2:53 So we can just say elapsed is the end_time minus the start_time. 2:58 And let's go ahead, And print out how long elapsed. 3:05 There we go, this is our SimpleBenchmarker. 3:15 So how do we run this? 3:17 Well, we'll have to instantiate the class and assign it to a variable. 3:19 And now we can say benchmarker.run. 3:27 Now, anything we do in here is going to be printed out with 3:38 the amount of time it took. 3:43 So, let's just say 100.times. 3:46 We'll multiply 1,000 times 1,000. 3:50 And I am gonna save that. 3:56 I click down here, exit out of irb, clear my screen. 4:00 And if I run simple_benchmarker.rb, we can see not very long elapsed. 4:04 Because computers are really fast, and 4:10 they can do math a lot quicker than we can. 4:13 So let's go ahead and change this code. 4:17 And we'll just say we're gonna do something 5.times. 4:20 What I'm gonna do is tell the computer to sleep for a random amount of time. 4:26 We do that by using the sleep method. 4:33 And what sleep is gonna do is nothing. 4:36 It's just going to pause the interpretation of the code, 4:38 just kinda hang out. 4:42 And now I'm going to say, using the rand method, 4:44 between 0.1 and 1.0. 4:47 So all this piece of code is gonna do is just pause the computer, 4:53 the execution time, for a random amount of time between 0.1 seconds and 1.0 seconds. 4:57 These are a couple new methods, just trust me that they work. 5:05 And we could look them up in the documentation if we wanted. 5:08 So let me clear my screen here. 5:11 If we run this benchmarker again, we can see it's not doing anything. 5:14 And now we have the elapsed time of 3.13 seconds. 5:19 Let's go ahead and just write out seconds, to be safe. 5:24 If we wanted to, we could get a little bit more fancy with this and 5:29 print out a dot so we know how long each iteration is taking. 5:33 And that time, it only took 2.8 seconds. 5:43 In fact, 5:48 if we wanted to we could print out a new line here just to separate all those dots. 5:49 That looks a little bit better. 5:58 And then just for fun, let's go ahead and add a description. 6:01 And print out the description. 6:08 And then we could say benchmarker.run, sleep a random amount of time. 6:15 Clear the screen, run it one more time. 6:25 And that looks pretty good. 6:29
You need to sign up for Treehouse in order to download course files.Sign up