简体   繁体   English

在 Elixir 中匹配字符串的最快方法是什么?

[英]What is the fastest method to match strings in Elixir?

Between binary patterns & string concatenation, what is the fastest method to match strings?在二进制模式和字符串连接之间,匹配字符串的最快方法是什么?

Binary patterns二元模式

<<"test"::utf8, rest::bytes>> = "test string"

String concatenation字符串连接

"test" <> rest = "test string"

Both the snippets compile to exactly the same Erlang code so they'll run at exactly the same speed.两个片段都编译为完全相同的 Erlang 代码,因此它们将以完全相同的速度运行。 We can verify this using decompile-beam .我们可以使用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">>.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM