How to resolve the algorithm Rate counter step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Rate counter step by step in the V (Vlang) programming language
Table of Contents
Problem Statement
Of interest is the code that performs the actual measurements. Any other code (such as job implementation or dispatching) that is required to demonstrate the rate tracking is helpful, but not the focus. Multiple approaches are allowed (even preferable), so long as they can accomplish these goals: Be aware of the precision and accuracy limitations of your timing mechanisms, and document them if you can. See also: System time, Time a function
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Rate counter step by step in the V (Vlang) programming language
Source code in the v programming language
import rand
import time
// representation of time.Time is nanosecond, actual resolution system specific
struct RateStateS {
mut:
last_flush time.Time
period time.Duration
tick_count int
}
fn (mut p_rate RateStateS) tic_rate() {
p_rate.tick_count++
now := time.now()
if now-p_rate.last_flush >= p_rate.period {
// TPS Report
mut tps := 0.0
if p_rate.tick_count > 0 {
tps = f64(p_rate.tick_count) / (now-p_rate.last_flush).seconds()
}
println("$tps tics per second.")
// Reset
p_rate.tick_count = 0
p_rate.last_flush = now
}
}
fn something_we_do() {
time.sleep(time.Duration(i64(9e7) + rand.i64n(i64(2e7)) or {i64(0)})) // sleep about .1 second.
}
fn main() {
start := time.now()
mut rate_watch := RateStateS{
last_flush: start,
period: 5 * time.second,
}
// Loop for twenty seconds
mut latest := start
for latest-start < 20*time.second {
something_we_do()
rate_watch.tic_rate()
latest = time.now()
}
}
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Swift programming language
You may also check:How to resolve the algorithm Sort a list of object identifiers step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Swift programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Stirling numbers of the second kind step by step in the Quackery programming language