birl
birl copied to clipboard
Timing off by factor of 100 on windows
At least, I assume it's a windows issue. I tested it on a Ubuntu system and did not have the same issue.
Minimal Example:
import birl
import birl/duration
import gleam/erlang/process
import gleam/io
import gleam/string
// Based on https://github.com/bcpeinhardt/learn_otp_with_gleam/blob/681c23174fb24d0f7e92ca581fd71306eb4d3063/src/tasks.gleam#L158
fn time(name: String, f: fn() -> a) -> a {
let start = birl.now()
let x = f()
let end = birl.now()
io.println(name <> " start:\t" <> birl.to_naive_time_string(start))
io.println(name <> " end: \t" <> birl.to_naive_time_string(end))
let difference = birl.difference(end, start) |> duration.decompose
io.println(name <> " took: \t" <> string.inspect(difference))
x
}
pub fn main() {
// Sleep for 2 seconds
time("timing test", fn() { process.sleep(2000) })
}
Output on Ubuntu system (expected):
timing test start: 15:07:18.823
timing test end: 15:07:20.853
timing test took: [#(2, Second), #(28, MilliSecond), #(539, MicroSecond)]
Output on Windows system:
timing test start: 15:07:51.380
timing test end: 15:07:53.399
timing test took: [#(20, MilliSecond), #(174, MicroSecond)]
As you can see, printing the time works fine. It's just the difference that is the issue.
I'm not experienced enough to diagnose exactly what is going on, but I assume that the Duration type that normally takes microseconds is being given 100 microsecond intervals.