Created
October 28, 2016 08:00
-
-
Save bchase/801bb99efdc11cfaba3b424ef7474464 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Web do | |
| # $ elixir -v | |
| # Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false] | |
| # | |
| # Elixir 1.3.4 | |
| # MATCH CASES | |
| # *** &Web.cond_and_regex_match/1 *** | |
| # 1.6 sec 2M iterations 0.78 μs/op | |
| # | |
| # *** &Web.string_split_and_map_get/1 *** | |
| # 1.2 sec 2M iterations 0.59 μs/op | |
| # | |
| # NO MATCH CASES | |
| # *** &Web.cond_and_regex_match/1 *** | |
| # 1.5 sec 2M iterations 0.73 μs/op | |
| # | |
| # *** &Web.string_split_and_map_get/1 *** | |
| # 1.2 sec 2M iterations 0.59 μs/op | |
| defmodule Endpoint1 do; end | |
| defmodule Endpoint2 do; end | |
| defmodule Endpoint3 do; end | |
| defmodule Endpoint4 do; end | |
| defmodule Endpoint5 do; end | |
| def cond_and_regex_match(path) do | |
| cond do | |
| # path =~ ~r{/bar} -> Endpoint2 | |
| # path =~ ~r{/baz} -> Endpoint3 | |
| # path =~ ~r{/boo} -> Endpoint4 | |
| path =~ ~r{/foo} -> Endpoint1 | |
| true -> Endpoint5 | |
| end | |
| end | |
| @path_prefixes %{"foo" => Endpoint1} | |
| def string_split_and_map_get(path) do | |
| [_, prefix | _] = String.split(path, "/") | |
| Map.get @path_prefixes, prefix, Endpoint5 | |
| end | |
| def benchmark do | |
| funcs = [ | |
| &__MODULE__.cond_and_regex_match/1, | |
| &__MODULE__.string_split_and_map_get/1 | |
| ] | |
| IO.puts "MATCH CASES" | |
| path = "/foo/bar/123/baz" | |
| Benchwarmer.benchmark funcs, path | |
| IO.puts "NO MATCH CASES" | |
| path = "/foo/bar/123/baz" | |
| Benchwarmer.benchmark funcs, path | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Web | |
| module Endpoint1; end | |
| module Endpoint2; end | |
| module Endpoint3; end | |
| module Endpoint4; end | |
| module Endpoint5; end | |
| def self.cond_and_regex_match(path) | |
| ### "cond + regex match" equiv ### | |
| if path =~ %r[/foo] then Web::Endpoint1 | |
| # elsif path =~ %r[/bar] then Web::Endpoint2 | |
| # elsif path =~ %r[/baz] then Web::Endpoint3 | |
| # elsif path =~ %r[/boo] then Web::Endpoint4 | |
| else Web::Endpoint5 end | |
| # alternatively `case` | |
| end | |
| @@path_prefixes = { "foo" => Web::Endpoint1 } | |
| def self.string_split_and_map_get(path) | |
| ### "String.split/2 + Map.get/3" equiv ### | |
| _, prefix = path.split(?/) # path.split(?/)[1] | |
| @@path_prefixes.fetch(prefix, Web::Endpoint5) | |
| end | |
| end | |
| path = "/foo/bar/123/baz" | |
| puts Web.cond_and_regex_match(path) | |
| puts Web.cond_and_regex_match("miss") | |
| puts Web.string_split_and_map_get(path) | |
| puts Web.string_split_and_map_get("miss") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment