How to calculate how many cpu resource was function spend?

I’m attempting to calculate the CPU resources consumed by my function. In other languages, I can utilize the getrusage syscall wrapper or alternatives for this purpose. However, in Elixir (Erlang), I haven’t found a similar function. I tried using :cpu_sup.util(), but it returns unexpected results when the workload is small (presumably correct, but not what I’m looking for).

Example:

defmodule X do
def run(c) do
    start = :cpu_sup.util()
    :crypto.strong_rand_bytes(c) |> Base.encode64()
    stop = :cpu_sup.util()
    dbg(start)
    dbg(stop)
    :ok
   end
end

Output:

iex> X.run(10000)
start #=> 0.8936825885978429
stop #=> 100.0

iex> X.run(1000000000)
start #=> 0.578392894030159
stop #=> 12.840227587267414

What is the proper way to determine the CPU resources expended while executing a function?

Probably, the best solution is use

:erlang.statistics(:runtime)

because only there I found getrusage call and calc time