Skip to content

Instantly share code, notes, and snippets.

@ewildgoose
Last active August 29, 2015 14:19
Show Gist options
  • Save ewildgoose/77a58681937cdf5441d7 to your computer and use it in GitHub Desktop.
Save ewildgoose/77a58681937cdf5441d7 to your computer and use it in GitHub Desktop.
defmodule BasicBench do
use Benchfella
@string_short 1..10 |> Enum.reduce( "", fn(_, acc) -> "a" <> acc end )
@string_100 1..100 |> Enum.reduce( "", fn(_, acc) -> "a" <> acc end )
@string_1000 1..1_000 |> Enum.reduce( "", fn(_, acc) -> "a" <> acc end )
@string_10k 1..10_000 |> Enum.reduce( "", fn(_, acc) -> "a" <> acc end )
@string_100k 1..10 |> Enum.reduce( "", fn(x, acc) -> @string_10k <> acc end )
bench "Orig - short" do
String.Unicode.codepoints @string_short
end
bench "Orig - 100" do
String.Unicode.codepoints @string_100
end
bench "Orig - 1K" do
String.Unicode.codepoints @string_1000
end
bench "Orig - 10K" do
String.Unicode.codepoints @string_10k
end
bench "Orig - 100K" do
String.Unicode.codepoints @string_100k
end
bench "With Reverse - short" do
String.Test2.codepoints @string_short
end
bench "With Reverse - 100" do
String.Test2.codepoints @string_100
end
bench "With Reverse - 1K" do
String.Test2.codepoints @string_1000
end
bench "With Reverse - 10K" do
String.Test2.codepoints @string_10k
end
bench "With Reverse - 100K" do
String.Test2.codepoints @string_100k
end
bench "With Stream - short" do
String.Test3.codepoints @string_short
end
bench "With Stream - 100" do
String.Test3.codepoints @string_100
end
bench "With Stream - 1K" do
String.Test3.codepoints @string_1000
end
bench "With Stream - 10K" do
String.Test3.codepoints @string_10k
end
bench "With Stream - 100K" do
String.Test3.codepoints @string_100k
end
end
defmodule String.Test2 do
def codepoints(binary) when is_binary(binary) do
do_codepoints(String.Unicode.next_codepoint(binary), [])
end
defp do_codepoints({c, rest}, accum) do
do_codepoints(String.Unicode.next_codepoint(rest), [c|accum])
end
defp do_codepoints(nil, accum) do
Enum.reverse(accum)
end
end
defmodule String.Test3 do
def codepoints(binary) when is_binary(binary) do
binary |> codepoint_stream |> Enum.to_list()
end
def codepoint_stream(binary) do
Stream.unfold(binary, fn(b) -> String.Unicode.next_codepoint(b) end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment