1 00:00:00,470 --> 00:00:02,870 In this video, we will calculate a delta 2 00:00:02,870 --> 00:00:05,749 time using instants. We've already seen 3 00:00:05,749 --> 00:00:07,939 how to get a duration by calling the 4 00:00:07,939 --> 00:00:10,730 elapsed method, this time let's get a 5 00:00:10,730 --> 00:00:12,799 delta time for each iteration through 6 00:00:12,799 --> 00:00:15,589 the loop, by doing arithmetic on instants 7 00:00:15,589 --> 00:00:18,500 directly. First let's make a mutable 8 00:00:18,500 --> 00:00:21,769 variable that represents the instant the 9 00:00:21,769 --> 00:00:24,740 last time we went through the loop, then 10 00:00:24,740 --> 00:00:27,619 every time right after we receive some 11 00:00:27,619 --> 00:00:30,939 bytes we calculate the instant right now. 12 00:00:30,939 --> 00:00:34,879 The duration is simply now - last 13 00:00:34,879 --> 00:00:37,879 instant, it's always later instant - 14 00:00:37,879 --> 00:00:41,059 earlier instant. I'm going to use that 15 00:00:41,059 --> 00:00:43,760 duration inside of my calculation for 16 00:00:43,760 --> 00:00:46,340 rate per second, which is going to be the 17 00:00:46,340 --> 00:00:50,030 number of bytes as a float 64, divided by 18 00:00:50,030 --> 00:00:54,280 the duration also cast to a float 64. 19 00:00:54,280 --> 00:00:56,930 Durations have a bunch of Handy methods 20 00:00:56,930 --> 00:00:57,980 for converting to different 21 00:00:57,980 --> 00:00:59,629 representations, so you can convert to 22 00:00:59,629 --> 00:01:04,099 seconds as F 32 or F 64, or as in integer 23 00:01:04,099 --> 00:01:07,840 type, you can convert to milliseconds, or 24 00:01:07,840 --> 00:01:10,670 microseconds or even nanoseconds, 25 00:01:10,670 --> 00:01:12,980 although the precision on that may vary 26 00:01:12,980 --> 00:01:14,960 depending on your hardware. Once we've 27 00:01:14,960 --> 00:01:18,260 finished we set last instant to now, and 28 00:01:18,260 --> 00:01:21,680 our calculation is all ready. So let's go 29 00:01:21,680 --> 00:01:24,050 add that to our progress output down 30 00:01:24,050 --> 00:01:24,560 here. 31 00:01:24,560 --> 00:01:26,630 Let's put square brackets around the 32 00:01:26,630 --> 00:01:29,030 rate, and then since this is a float 33 00:01:29,030 --> 00:01:32,270 let's drop the fractional part. We're 34 00:01:32,270 --> 00:01:34,580 doing bytes per second in a fractional 35 00:01:34,580 --> 00:01:36,980 byte doesn't really make much sense, then 36 00:01:36,980 --> 00:01:38,750 we just need to go to the end of the 37 00:01:38,750 --> 00:01:42,760 ePrint statement, and add our variable. 38 00:01:42,760 --> 00:01:45,520 That's looking a little bit long, let's 39 00:01:45,520 --> 00:01:47,350 see if Cargo fmt would reformat this 40 00:01:47,350 --> 00:01:50,020 differently, let's just run it real quick, 41 00:01:50,020 --> 00:01:52,540 and then we'll switch back over and 42 00:01:52,540 --> 00:01:55,660 Cargo fmt agreed, it reformatted our 43 00:01:55,660 --> 00:01:57,790 arguments vertically. There's nothing 44 00:01:57,790 --> 00:02:00,100 else that we need to do code wise, so 45 00:02:00,100 --> 00:02:02,470 let's go test this out. Let's start with 46 00:02:02,470 --> 00:02:05,020 our familiar echo hello piped to Cargo 47 00:02:05,020 --> 00:02:06,600 run, 48 00:02:06,600 --> 00:02:09,479 we get our output which is interleaved 49 00:02:09,479 --> 00:02:12,389 with our progress, so let's run it again 50 00:02:12,389 --> 00:02:14,670 and this time let's pipe our output to 51 00:02:14,670 --> 00:02:18,150 Dev null, great, what is this extra 52 00:02:18,150 --> 00:02:20,670 little bit here? We've got this extra it 53 00:02:20,670 --> 00:02:22,920 looks like it's wrong, what happened here 54 00:02:22,920 --> 00:02:24,870 is after the first read we printed out 55 00:02:24,870 --> 00:02:27,390 some large number of bytes per second, 56 00:02:27,390 --> 00:02:30,150 and then we received our Sentinel, our 0 57 00:02:30,150 --> 00:02:32,220 value on the second read, and it printed 58 00:02:32,220 --> 00:02:34,560 out a short value a zero bytes per 59 00:02:34,560 --> 00:02:36,420 second because we received a zero bytes, 60 00:02:36,420 --> 00:02:38,640 and so we've got leftover characters, 61 00:02:38,640 --> 00:02:40,800 because for the first time we've got us 62 00:02:40,800 --> 00:02:42,959 an output line near the end that's 63 00:02:42,959 --> 00:02:45,209 shorter than an output line at the start. 64 00:02:45,209 --> 00:02:46,890 We're going to fix both of those 65 00:02:46,890 --> 00:02:49,350 problems, both the printing zero at the 66 00:02:49,350 --> 00:02:51,239 end because we receive the Sentinel, we 67 00:02:51,239 --> 00:02:52,680 don't really care about that, it'd be 68 00:02:52,680 --> 00:02:54,900 better to leave it with the last good 69 00:02:54,900 --> 00:02:56,880 value, and we also don't want to have 70 00:02:56,880 --> 00:02:58,769 extra characters left over at the end of 71 00:02:58,769 --> 00:03:00,569 the line, so like I said we'll fix that 72 00:03:00,569 --> 00:03:02,640 in a later video, but for this video 73 00:03:02,640 --> 00:03:05,730 that's okay. Let's see our rate in action 74 00:03:05,730 --> 00:03:08,640 here's our now familiar yes pipe and 75 00:03:08,640 --> 00:03:11,400 there we go looks like it's working. You 76 00:03:11,400 --> 00:03:13,620 can see all three parts of our progress 77 00:03:13,620 --> 00:03:16,680 output incorrectly just control C this 78 00:03:16,680 --> 00:03:19,230 and we're done for this video. In the 79 00:03:19,230 --> 00:03:21,239 next video, we will use durations to 80 00:03:21,239 --> 00:03:26,239 create an ergonomic timer struct.