<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="68049" data-post-id="68049">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="michalmuskala" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/120/20288_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  michalmuskala
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="OvermindDL1" data-post="9" data-topic="12063">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>(doing <code>&lt;&lt;_:binary&gt;&gt;</code> is faster than doing s when is_binary(s) for one example)</p>
</blockquote>
</aside>
<p>This is not true. At least not on today’s BEAM. Matching on <code>&lt;&lt;_:binary&gt;&gt;</code> is going to be faster, if you’re doing a binary pattern match already, to keep the “single match context” optimization. But if you’re only checking if something is a binary, it’s going to be slower. This is primarily because it needs to allocate the match context (which creates garbage on the heap that will lead to sooner garbage collection), while checking <code>is_binary</code> does not involve any memory movements. This memory access (and GC jitter) can be seen in the increased  deviation of the benchmark.</p>
<pre data-code-wrap="raw"><code class="lang-raw">Operating System: macOS
CPU Information: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.6.0-dev
Erlang 20.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
parallel: 1
inputs: binary, not binary
Estimated total run time: 28 s

##### With input binary #####
Name            ips        average  deviation         median         99th %
guard      436.27 K        2.29 μs    ±34.28%        2.10 μs        4.20 μs
match      236.41 K        4.23 μs  ±1391.73%           4 μs          11 μs

Comparison:
guard      436.27 K
match      236.41 K - 1.85x slower

##### With input not binary #####
Name            ips        average  deviation         median         99th %
guard      448.69 K        2.23 μs    ±35.38%           2 μs        4.40 μs
match      431.08 K        2.32 μs    ±43.81%        2.20 μs        4.60 μs

Comparison:
guard      448.69 K
match      431.08 K - 1.04x slower
</code></pre>
<p>The benchmark code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Bench do
  def match(&lt;&lt;_::binary&gt;&gt;), do: :binary
  def match(_), do: :not_binary

  def guard(binary) when is_binary(binary), do: :binary
  def guard(_), do: :not_binary

  def loop(0, _fun) do
    :ok
  end
  def loop(n, fun) do
    fun.()
    loop(n - 1, fun)
  end
end

jobs = %{
  "match" =&gt; fn input -&gt; Bench.loop(100, fn -&gt; Bench.match(input) end) end,
  "guard" =&gt; fn input -&gt; Bench.loop(100, fn -&gt; Bench.guard(input) end) end
}

inputs = %{
  "binary" =&gt; "foo",
  "not binary" =&gt; 123
}

Benchee.run(jobs, inputs: inputs)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="68049" data-batch-url="/posts/batch_likers">
                        7
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/structs-pattern-matching-on-nil-values/12063/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-68049" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="68049"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="68096" data-post-id="68096">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="michalmuskala" data-post="12" data-topic="12063">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/48/20288_2.png" class="avatar"> michalmuskala:</div>
<blockquote>
<p>This is not true. At least not on today’s BEAM. Matching on &lt;&lt;_:binary&gt;&gt; is going to be faster, if you’re doing a binary pattern match already, to keep the “single match context” optimization. But if you’re only checking if something is a binary, it’s going to be slower.</p>
</blockquote>
</aside>
<p>Ooo really?  Interesting…  Back in the day just by swapping from guards to match’s of precisely that style on erlang more than doubled the speed of things I made that were head-heavy.  At least back then I remember Guards being run ‘after’ the match tree had been run, so if you had any kind of sizeable head-count for functions/cases then lifting more into the match context would be quite valuable.  Fascinating though, thanks for the benchmark, though I wonder if that holds true with large head counts still?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="68096" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/structs-pattern-matching-on-nil-values/12063/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-68096" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="68096"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="68097" data-post-id="68097">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ahh, I think I was thinking about lists (as lists are the default ‘string’ type in erlang):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">##### With input binary #####
Name                ips        average  deviation         median         99th %
guard_top       34.09 K       29.34 μs    ±74.54%          28 μs          45 μs
guard_bot       30.61 K       32.67 μs    ±74.42%          31 μs          48 μs
match_bot       22.81 K       43.85 μs    ±35.95%          41 μs          78 μs
match_top       22.38 K       44.69 μs    ±38.02%          43 μs          67 μs

Comparison: 
guard_top       34.09 K
guard_bot       30.61 K - 1.11x slower
match_bot       22.81 K - 1.49x slower
match_top       22.38 K - 1.52x slower

##### With input list #####
Name                ips        average  deviation         median         99th %
match_bot       34.39 K       29.08 μs    ±65.78%          28 μs          41 μs
guard_top       34.24 K       29.21 μs    ±54.19%          28 μs          45 μs
match_top       31.49 K       31.75 μs    ±61.61%          31 μs          46 μs
guard_bot       31.25 K       32.00 μs    ±57.29%          31 μs          49 μs

Comparison: 
match_bot       34.39 K
guard_top       34.24 K - 1.00x slower
match_top       31.49 K - 1.09x slower
guard_bot       31.25 K - 1.10x slower

##### With input not binary #####
Name                ips        average  deviation         median         99th %
guard_bot       31.18 K       32.07 μs    ±63.11%          31 μs          44 μs
guard_top       31.15 K       32.10 μs    ±58.90%          30 μs          50 μs
match_bot       29.16 K       34.30 μs    ±38.15%          33 μs          49 μs
match_top       27.41 K       36.48 μs    ±36.63%          35 μs          52 μs

Comparison: 
guard_bot       31.18 K
guard_top       31.15 K - 1.00x slower
match_bot       29.16 K - 1.07x slower
match_top       27.41 K - 1.14x slower
</code></pre>
<p>Substantial performance difference on binaries though (a good 50% slower for match contexts)!  And of course the list test is faster than the binary test regardless of how it is done (the BEAM is built for lists instead of binaries when processing them out).  ^.^</p>
<p>EDIT:  The code is about the same is <a class="mention" href="/u/michalmuskala" rel="nofollow">@michalmuskala</a>’s except the count is increased to 1000 to prevent fast function warnings and I added some tuple cases to each set (unmatched in all cases) with the string/list test being at the top or bottom of the tuple matchers as per the test name.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="68097" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/structs-pattern-matching-on-nil-values/12063/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-68097" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="68097"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>