3
在Python中,您可以使用timeit模塊爲代碼執行一小段執行時間。Elixir中是否有python timeit的等效模塊?
https://docs.python.org/2/library/timeit.html
是否有藥劑的相同呢?
在Python中,您可以使用timeit模塊爲代碼執行一小段執行時間。Elixir中是否有python timeit的等效模塊?
https://docs.python.org/2/library/timeit.html
是否有藥劑的相同呢?
最簡單的工具是使用Erlang的:timer
模塊,其中一個是tc
變體。它以微秒爲單位返回執行時間,並將函數的結果作爲元組返回。到時候匿名函數,你可以做
{time, res} = :timer.tc fn -> :timer.sleep(1000) end
# {1000575, :ok}
{time, res} = :timer.tc(fn(ms) -> :timer.sleep(ms) end, [1000])
# {1001283, :ok}
時間模塊的功能,你可以做
defmodule Sleepy do
def sleep(ms) do
:timer.sleep(ms)
end
end
{time, res} = :timer.tc(&Sleepy.sleep/1, [1000])
# {1001106, :ok}
{time, res} = :timer.tc(Sleepy, :sleep, [1000])
# {1000352, :ok}
Timex是另一種選擇,這對於測量時間,除其他事項外一個友好的API,但如果所有你需要的是時間,:timer
綽綽有餘的一些包裝代碼。
我知道這不是執行時機,但是如果您試圖提高代碼性能,這也可能有所幫助:http://stackoverflow.com/questions/204926/profiler-analyzer-for-erlang – 2014-10-01 13:32:01