【发布时间】:2018-07-05 09:03:24
【问题描述】:
在二进制模式和字符串连接之间,匹配字符串最快的方法是什么?
二进制模式
<<"test"::utf8, rest::bytes>> = "test string"
字符串连接
"test" <> rest = "test string"
【问题讨论】:
标签: string pattern-matching elixir
在二进制模式和字符串连接之间,匹配字符串最快的方法是什么?
<<"test"::utf8, rest::bytes>> = "test string"
"test" <> rest = "test string"
【问题讨论】:
标签: string pattern-matching elixir
两个 sn-ps 都编译为完全相同的 Erlang 代码,因此它们将以完全相同的速度运行。我们可以使用decompile-beam 来验证这一点。
$ cat a.exs
defmodule A do
def a do
<<"test"::utf8, rest::bytes>> = "test string"
end
def b do
"test" <> rest = "test string"
end
end
$ elixirc a.exs
$ decompile-beam Elixir.A.beam
...
a() ->
<<"test"/utf8, rest@1/binary>> = <<"test string">>.
b() -> <<"test", rest@1/binary>> = <<"test string">>.
【讨论】: