1 00:00:00,698 --> 00:00:04,860 One way that blocks are really useful is performing functionality or 2 00:00:04,860 --> 00:00:08,030 running code before and after a block is executed. 3 00:00:09,090 --> 00:00:12,340 This turns out to be really great for writing something like a benchmarker. 4 00:00:13,360 --> 00:00:16,530 We're going to write a simple one now, and here's how we're gonna do it. 5 00:00:17,730 --> 00:00:21,080 First, we'll create a class for our benchmarker. 6 00:00:21,080 --> 00:00:24,190 The class will have one method called run. 7 00:00:24,190 --> 00:00:25,870 The run method will take a block. 8 00:00:27,570 --> 00:00:30,400 We'll take the current time before the block of code is run and 9 00:00:30,400 --> 00:00:32,590 store it in a variable. 10 00:00:32,590 --> 00:00:34,660 Then we'll run the block, and 11 00:00:34,660 --> 00:00:37,490 store the current time after the block of code is executed. 12 00:00:38,550 --> 00:00:42,960 From there, we only need to subtract the end time from the start time and 13 00:00:42,960 --> 00:00:43,670 print the result. 14 00:00:45,360 --> 00:00:46,880 Go ahead and give it a try now. 15 00:00:46,880 --> 00:00:49,106 And then come back and take a look to see how I did. 16 00:00:49,106 --> 00:00:51,170 And here's a hint. 17 00:00:51,170 --> 00:00:54,800 You can use the method Time.now to get the current time. 18 00:00:56,210 --> 00:00:59,920 Okay, so let's go ahead and build our SimpleBenchmarker class. 19 00:00:59,920 --> 00:01:04,270 Now we're going to use a new part of the standard library that we haven't used 20 00:01:04,270 --> 00:01:07,520 before, called the Time class. 21 00:01:07,520 --> 00:01:10,820 And Time is pretty interesting. 22 00:01:10,820 --> 00:01:13,670 Now, that's not a metaphor, I mean the actual Time class. 23 00:01:13,670 --> 00:01:20,590 We're only really interested in one particular method from the Time class. 24 00:01:20,590 --> 00:01:23,580 And let's go ahead and scroll down here. 25 00:01:23,580 --> 00:01:25,400 We're looking at the now method, 26 00:01:27,300 --> 00:01:31,300 which creates a new Time object for the current time. 27 00:01:32,640 --> 00:01:37,650 Let's go ahead and see how that works in irb, before we start coding our class. 28 00:01:38,970 --> 00:01:40,770 And just see how that works real quick. 29 00:01:40,770 --> 00:01:48,420 We call Time.now, and it returns the current time. 30 00:01:48,420 --> 00:01:52,830 So this is something that we're going to use in our SimpleBenchmarker class. 31 00:01:52,830 --> 00:01:57,992 So we're going to create a new file and call it simple_benchmarker.rb. 32 00:01:57,992 --> 00:02:01,030 And that opens it up for us. 33 00:02:01,030 --> 00:02:04,342 So now we can create our SimpleBenchmarker class. 34 00:02:09,841 --> 00:02:13,882 And we're going to create a method called run. 35 00:02:16,622 --> 00:02:17,990 And that's gonna take a block. 36 00:02:17,990 --> 00:02:22,040 And what it's gonna do is run this block of code, and 37 00:02:22,040 --> 00:02:25,430 then print out the time it took to run that code. 38 00:02:26,520 --> 00:02:29,930 So we can do that by saying yield or block.call. 39 00:02:29,930 --> 00:02:32,250 We'll just use block.call here. 40 00:02:32,250 --> 00:02:35,200 No real reason behind that, just something that we're gonna do. 41 00:02:37,410 --> 00:02:43,235 And let's go ahead and get the start_time before we call the block, and 42 00:02:43,235 --> 00:02:49,560 the end_time after the block is called. 43 00:02:49,560 --> 00:02:53,220 So once we do that, we have the end_time and the start_time. 44 00:02:53,220 --> 00:02:56,670 All we need to do is subtract the two, and we'll know the elapsed time. 45 00:02:58,400 --> 00:03:04,100 So we can just say elapsed is the end_time minus the start_time. 46 00:03:05,250 --> 00:03:12,470 And let's go ahead, And print out how long elapsed. 47 00:03:15,360 --> 00:03:17,670 There we go, this is our SimpleBenchmarker. 48 00:03:17,670 --> 00:03:19,390 So how do we run this? 49 00:03:19,390 --> 00:03:24,590 Well, we'll have to instantiate the class and assign it to a variable. 50 00:03:27,940 --> 00:03:33,292 And now we can say benchmarker.run. 51 00:03:38,492 --> 00:03:43,052 Now, anything we do in here is going to be printed out with 52 00:03:43,052 --> 00:03:46,230 the amount of time it took. 53 00:03:46,230 --> 00:03:50,136 So, let's just say 100.times. 54 00:03:50,136 --> 00:03:56,522 We'll multiply 1,000 times 1,000. 55 00:03:56,522 --> 00:04:00,522 And I am gonna save that. 56 00:04:00,522 --> 00:04:04,623 I click down here, exit out of irb, clear my screen. 57 00:04:04,623 --> 00:04:10,783 And if I run simple_benchmarker.rb, we can see not very long elapsed. 58 00:04:10,783 --> 00:04:13,744 Because computers are really fast, and 59 00:04:13,744 --> 00:04:17,780 they can do math a lot quicker than we can. 60 00:04:17,780 --> 00:04:19,520 So let's go ahead and change this code. 61 00:04:20,790 --> 00:04:24,145 And we'll just say we're gonna do something 5.times. 62 00:04:26,780 --> 00:04:32,030 What I'm gonna do is tell the computer to sleep for a random amount of time. 63 00:04:33,700 --> 00:04:36,090 We do that by using the sleep method. 64 00:04:36,090 --> 00:04:38,120 And what sleep is gonna do is nothing. 65 00:04:38,120 --> 00:04:42,730 It's just going to pause the interpretation of the code, 66 00:04:42,730 --> 00:04:44,120 just kinda hang out. 67 00:04:44,120 --> 00:04:47,666 And now I'm going to say, using the rand method, 68 00:04:47,666 --> 00:04:52,381 between 0.1 and 1.0. 69 00:04:53,560 --> 00:04:57,720 So all this piece of code is gonna do is just pause the computer, 70 00:04:57,720 --> 00:05:05,047 the execution time, for a random amount of time between 0.1 seconds and 1.0 seconds. 71 00:05:05,047 --> 00:05:08,360 These are a couple new methods, just trust me that they work. 72 00:05:08,360 --> 00:05:10,490 And we could look them up in the documentation if we wanted. 73 00:05:11,490 --> 00:05:12,430 So let me clear my screen here. 74 00:05:14,640 --> 00:05:17,580 If we run this benchmarker again, we can see it's not doing anything. 75 00:05:19,250 --> 00:05:24,222 And now we have the elapsed time of 3.13 seconds. 76 00:05:24,222 --> 00:05:26,650 Let's go ahead and just write out seconds, to be safe. 77 00:05:29,410 --> 00:05:33,444 If we wanted to, we could get a little bit more fancy with this and 78 00:05:33,444 --> 00:05:37,197 print out a dot so we know how long each iteration is taking. 79 00:05:43,096 --> 00:05:45,770 And that time, it only took 2.8 seconds. 80 00:05:48,450 --> 00:05:49,050 In fact, 81 00:05:49,050 --> 00:05:54,610 if we wanted to we could print out a new line here just to separate all those dots. 82 00:05:58,840 --> 00:06:01,131 That looks a little bit better. 83 00:06:01,131 --> 00:06:04,712 And then just for fun, let's go ahead and add a description. 84 00:06:08,192 --> 00:06:09,691 And print out the description. 85 00:06:15,952 --> 00:06:20,891 And then we could say benchmarker.run, sleep a random amount of time. 86 00:06:25,211 --> 00:06:27,153 Clear the screen, run it one more time. 87 00:06:29,333 --> 00:06:30,730 And that looks pretty good.