【问题标题】:What is the fastest method to match strings in Elixir?在 Elixir 中匹配字符串的最快方法是什么?
【发布时间】:2018-07-05 09:03:24
【问题描述】:

在二进制模式和字符串连接之间,匹配字符串最快的方法是什么?

二进制模式

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

字符串连接

"test" <> rest = "test string"

【问题讨论】:

    标签: string pattern-matching elixir


    【解决方案1】:

    两个 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">>.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2011-09-26
      相关资源
      最近更新 更多