Skip to content

Commit 5a455f1

Browse files
committed
adds a benchfella benchmark
problem: - in PR #5 I wanted to compare different implementations for underscoring solution: - use benchfella package - small script results: - the Macro.underscore version is roughly 2x faster... that time adds up on larger json payloads, so I'll keep it for now. It's always possible to switch it to something custom later, if that becomes a problem. ``` ## BasicBench macro_underscore - short 100000 11.23 µs/op regex_underscore - short 100000 26.51 µs/op macro_underscore - long 50000 45.81 µs/op regex_underscore - long 20000 75.77 µs/op ```
1 parent 5a89276 commit 5a455f1

5 files changed

Lines changed: 55 additions & 2 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: fa
2626
# underscoring can be turned off by passing `underscore: false` to opts
2727
iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: false )
2828
%{CamelCase: [%{c: 1}, %{c: 2}]}
29+
30+
# hyphens are replaced
31+
iex> AtomicMap.convert(%{ "some-key" => [ %{"c" => 1}, %{"c" => 2}] }, safe: false, underscore: true )
32+
%{some_key: [%{c: 1}, %{c: 2}]}
2933
```
3034

3135

@@ -38,3 +42,8 @@ iex> AtomicMap.convert(%{ "CamelCase" => [ %{"c" => 1}, %{"c" => 2}] }, safe: fa
3842

3943
## Todo:
4044
- maybe allow direct conversion to a struct, like Poison does it: as: %SomeStruct{}...
45+
46+
47+
## Benchmark
48+
49+
$ mix bench

bench/basic_bench.exs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
defmodule BasicBench do
2+
use Benchfella
3+
4+
def macro_underscore(s) do
5+
s
6+
|> Macro.underscore
7+
|> String.replace(~r/-/, "_")
8+
end
9+
10+
def regex_underscore(s) do
11+
s
12+
|> String.replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2")
13+
|> String.replace(~r/([a-z\d])([A-Z])/, "\\1_\\2")
14+
|> String.replace(~r/-/, "_")
15+
|> String.downcase
16+
end
17+
18+
@long_string "StringShouldChange-some_stuffStringShouldChange-some_stuffStringShouldChange-some_stuffStringShouldChange-some_stuffStringShouldChange-some_stuff"
19+
@short_string "StringShouldChange-some_stuff"
20+
21+
bench "macro_underscore - long" do
22+
@long_string |> macro_underscore
23+
end
24+
25+
bench "regex_underscore - long" do
26+
@long_string |> regex_underscore
27+
end
28+
29+
bench "macro_underscore - short" do
30+
@short_string |> macro_underscore
31+
end
32+
33+
bench "regex_underscore - short" do
34+
@short_string |> regex_underscore
35+
end
36+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
duration:1.0;mem stats:false;sys mem stats:false
2+
module;test;tags;iterations;elapsed
3+
BasicBench regex_underscore - short 100000 2667305
4+
BasicBench regex_underscore - long 20000 1656602
5+
BasicBench macro_underscore - short 100000 1347365
6+
BasicBench macro_underscore - long 50000 2556233

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ defmodule AtomicMap.Mixfile do
3232
defp deps do
3333
[
3434
{:earmark, "~> 0.2", only: :dev},
35-
{:ex_doc, "~> 0.11", only: :dev}
35+
{:ex_doc, "~> 0.11", only: :dev},
36+
{:benchfella, "0.3.2", only: :dev},
3637
]
3738
end
3839

mix.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
%{"earmark": {:hex, :earmark, "0.2.1"},
1+
%{"benchfella": {:hex, :benchfella, "0.3.2"},
2+
"earmark": {:hex, :earmark, "0.2.1"},
23
"ex_doc": {:hex, :ex_doc, "0.11.4"}}

0 commit comments

Comments
 (0)