In this video, we will calculate a delta
time using instants. We've already seen
how to get a duration by calling the
elapsed method, this time let's get a
delta time for each iteration through
the loop, by doing arithmetic on instants
directly. First let's make a mutable
variable that represents the instant the
last time we went through the loop, then
every time right after we receive some
bytes we calculate the instant right now.
The duration is simply now - last
instant, it's always later instant -
earlier instant. I'm going to use that
duration inside of my calculation for
rate per second, which is going to be the
number of bytes as a float 64, divided by
the duration also cast to a float 64.
Durations have a bunch of Handy methods
for converting to different
representations, so you can convert to
seconds as F 32 or F 64, or as in integer
type, you can convert to milliseconds, or
microseconds or even nanoseconds,
although the precision on that may vary
depending on your hardware. Once we've
finished we set last instant to now, and
our calculation is all ready. So let's go
add that to our progress output down
here.
Let's put square brackets around the
rate, and then since this is a float
let's drop the fractional part. We're
doing bytes per second in a fractional
byte doesn't really make much sense, then
we just need to go to the end of the
ePrint statement, and add our variable.
That's looking a little bit long, let's
see if Cargo fmt would reformat this
differently, let's just run it real quick,
and then we'll switch back over and
Cargo fmt agreed, it reformatted our
arguments vertically. There's nothing
else that we need to do code wise, so
let's go test this out. Let's start with
our familiar echo hello piped to Cargo
run,
we get our output which is interleaved
with our progress, so let's run it again
and this time let's pipe our output to
Dev null, great, what is this extra
little bit here? We've got this extra it
looks like it's wrong, what happened here
is after the first read we printed out
some large number of bytes per second,
and then we received our Sentinel, our 0
value on the second read, and it printed
out a short value a zero bytes per
second because we received a zero bytes,
and so we've got leftover characters,
because for the first time we've got us
an output line near the end that's
shorter than an output line at the start.
We're going to fix both of those
problems, both the printing zero at the
end because we receive the Sentinel, we
don't really care about that, it'd be
better to leave it with the last good
value, and we also don't want to have
extra characters left over at the end of
the line, so like I said we'll fix that
in a later video, but for this video
that's okay. Let's see our rate in action
here's our now familiar yes pipe and
there we go looks like it's working. You
can see all three parts of our progress
output incorrectly just control C this
and we're done for this video. In the
next video, we will use durations to
create an ergonomic timer struct.