Hello.
I want to measure time which run a time-consuming process in spawn, following is reduced code which i use sleep
to simulate the process time.
start_time = Time.monotonic
chan = Channel(Tuple(String, Int32)).new
def meth_4_seconds(chan, start_time)
sleep 4
chan.send({"method_4_seconds", (Time.monotonic - start_time).milliseconds})
end
def meth_2_seconds(chan, start_time)
sleep 2
chan.send({"meth_2_seconds", (Time.monotonic - start_time).milliseconds})
end
spawn meth_4_seconds(chan, start_time)
spawn meth_2_seconds(chan, start_time)
2.times do
p chan.receive
end
The really confused me is, except for the time is incorrect, even, the method_2_seconds spent more time than method_4_seconds:
╰─ $ crystal build 1.cr && ./1
{"meth_2_seconds", 2}
{"method_4_seconds", 1}
I guess i misunderstood the usage of something, how to fix that?
Thanks