ExUnit.start
ExUnit.configure trace: true
defmodule Foo do
def bar(0, acc), do: acc
def bar(n, acc), do: bar(n - 1, acc <> "1")
def baz(0, acc), do: acc
def baz(n, acc), do: baz(n - 1, "1" <> acc)
end
defmodule Test do
use ExUnit.Case
@num 1_000_000
test ~s(acc <> "1") do
Foo.bar(@num, "")
end
test ~s("1" <> acc) do
Foo.baz(@num, "")
end
end
结果
Test
* test acc <> "1" (42.1ms)
* test "1" <> acc (35733.3ms)
为什么将"1"
连接到长二进制acc
的开头比连续到结尾需要很长时间?