Created
October 22, 2025 09:25
-
-
Save katafrakt/d02158e1ff439effdad98452b315f23c 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
| Mix.install([:benchee]) | |
| defmodule Lookup do | |
| @lookup_table %{ | |
| "one" => :one, | |
| "two" => :two, | |
| "three" => :three | |
| } | |
| def by_table(value) do | |
| Map.get(@lookup_table, value, :more) | |
| end | |
| def by_function(value) do | |
| case value do | |
| "one" -> :one | |
| "two" -> :two | |
| "three" -> :three | |
| _ -> :more | |
| end | |
| end | |
| end | |
| Benchee.run( | |
| %{ | |
| "by_table, found value" => fn -> Lookup.by_table("three") end, | |
| "by_table, fallback" => fn -> Lookup.by_table("five") end, | |
| "by_function, found value" => fn -> Lookup.by_function("three") end, | |
| "by_function, fallback" => fn -> Lookup.by_function("five") end | |
| } | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment