Skip to content

Instantly share code, notes, and snippets.

@alco
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alco/b4f5d1d995005b521986 to your computer and use it in GitHub Desktop.
Save alco/b4f5d1d995005b521986 to your computer and use it in GitHub Desktop.
reduplicate
Benchfella.start duration: 1.0
defmodule StringDuplicateBench do
use Benchfella
Enum.each([100, 1000, 10000, 100000], fn n ->
@str "1234567890"
@n n
bench "binary copy #{n}" do
:binary.copy(@str, @n)
end
bench "strdup #{n}" do
strdup(@str, @n)
end
end)
# The same idea as in fast exponentiation by squaring is used here
def strdup(_, 0), do: ""
def strdup(str, 1), do: str
def strdup(str, n) when rem(n, 2) == 0 do
tmp = strdup(str, div(n, 2))
tmp <> tmp
end
def strdup(str, n) do
str <> strdup(str, n-1)
end
end
StringDuplicateBench.strdup nox erlang 100: 1000000 1.42 µs/op
StringDuplicateBench.strdup nox core 100: 1000000 1.46 µs/op
StringDuplicateBench.strdup nox core 1000: 1000000 2.23 µs/op
StringDuplicateBench.strdup nox erlang 1000: 1000000 2.50 µs/op
StringDuplicateBench.strdup nox core 10000: 100000 12.12 µs/op
StringDuplicateBench.strdup nox erlang 10000: 100000 15.03 µs/op
StringDuplicateBench.strdup nox erlang 100000: 10000 121.60 µs/op
StringDuplicateBench.strdup nox core 100000: 5000 450.42 µs/op
StringDuplicateBench.strdup nox core 1000000: 500 5122.71 µs/op
StringDuplicateBench.strdup nox erlang 1000000: 500 5712.28 µs/op
Benchfella.start duration: 1.0
defmodule StringDuplicateBench do
use Benchfella
Enum.each([100, 1000, 10000, 100000], fn n ->
@str "1234567890"
@n n
bench "binary copy #{n}" do
:binary.copy(@str, @n)
end
bench "strdup true_droid #{n}" do
:t.true_droid(@str, @n)
end
bench "strdup nox erlang #{n}" do
:t.nox_erlang(@str, @n)
end
bench "strdup nox core #{n}" do
:t2.nox_core(@str, @n)
end
end)
end
StringDuplicateBench.binary copy 100: 10000000 0.72 µs/op
StringDuplicateBench.strdup nox erlang 100: 1000000 1.42 µs/op
StringDuplicateBench.strdup nox core 100: 1000000 1.47 µs/op
StringDuplicateBench.strdup true_droid 100: 1000000 1.71 µs/op
StringDuplicateBench.strdup nox core 1000: 1000000 2.25 µs/op
StringDuplicateBench.strdup nox erlang 1000: 1000000 2.53 µs/op
StringDuplicateBench.strdup true_droid 1000: 500000 3.17 µs/op
StringDuplicateBench.binary copy 1000: 500000 5.54 µs/op
StringDuplicateBench.strdup true_droid 10000: 100000 10.08 µs/op
StringDuplicateBench.strdup nox core 10000: 100000 12.46 µs/op
StringDuplicateBench.strdup nox erlang 10000: 100000 15.87 µs/op
StringDuplicateBench.binary copy 10000: 50000 54.38 µs/op
StringDuplicateBench.strdup true_droid 100000: 20000 90.44 µs/op
StringDuplicateBench.strdup nox erlang 100000: 10000 120.18 µs/op
StringDuplicateBench.strdup nox core 100000: 5000 450.04 µs/op
StringDuplicateBench.binary copy 100000: 5000 543.66 µs/op
StringDuplicateBench.binary copy 100: 10000000 0.71 µs/op
StringDuplicateBench.strdup 100: 1000000 1.69 µs/op
StringDuplicateBench.strdup 1000: 500000 3.48 µs/op
StringDuplicateBench.binary copy 1000: 500000 5.41 µs/op
StringDuplicateBench.strdup 10000: 100000 11.29 µs/op
StringDuplicateBench.binary copy 10000: 50000 54.60 µs/op
StringDuplicateBench.strdup 100000: 20000 88.69 µs/op
StringDuplicateBench.binary copy 100000: 5000 546.27 µs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment