-
-
Save MSch/e16e23ee2d8bae649c86 to your computer and use it in GitHub Desktop.
This file contains 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
Benchfella.start duration: 1.0#, format: :machine | |
defmodule StringDuplicateBench do | |
use Benchfella | |
@iterations [100, 1000, 10000, 100000] | |
Enum.each(@iterations, fn n -> | |
@str "1234567890" | |
@n n | |
bench "binary copy #{n}" do | |
:binary.copy(@str, @n) | |
end | |
bench "strdup #{n}" do | |
strdup(@str, @n) | |
end | |
bench "List.duplicate #{n}" do | |
List.duplicate(@str, @n) | |
end | |
bench "iodata #{n}" do | |
iodatadup(@str, @n) | |
end | |
bench "iodata square #{n}" do | |
iodatasquaredup(@str, @n) | |
end | |
bench "iodata to binary #{n}" do | |
IO.iodata_to_binary(iodatadup(@str, @n)) | |
end | |
bench "iodata square to binary #{n}" do | |
IO.iodata_to_binary(iodatasquaredup(@str, @n)) | |
end | |
end) | |
def iodatadup(_, 0), do: [] | |
def iodatadup(str, n), do: [str|iodatadup(str, n-1)] | |
# The same idea as in fast exponentiation by squaring is used here | |
def iodatasquaredup(_, 0), do: [] | |
def iodatasquaredup(str, 1), do: [str] | |
def iodatasquaredup(str, n) when rem(n, 2) == 0 do | |
tmp = iodatasquaredup(str, div(n, 2)) | |
[tmp|tmp] | |
end | |
def iodatasquaredup(str, n) do | |
[str|iodatasquaredup(str, n-1)] | |
end | |
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 |
This file contains 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
StringDuplicateBench.iodata square 100: 10000000 0.29 µs/op | |
StringDuplicateBench.binary copy 100: 10000000 0.40 µs/op | |
StringDuplicateBench.iodata square 1000: 10000000 0.50 µs/op | |
StringDuplicateBench.iodata square 10000: 10000000 0.62 µs/op | |
StringDuplicateBench.iodata square 100000: 10000000 0.80 µs/op | |
StringDuplicateBench.List.duplicate 100: 10000000 0.96 µs/op | |
StringDuplicateBench.strdup 100: 1000000 1.11 µs/op | |
StringDuplicateBench.iodata square to binary 100: 1000000 1.32 µs/op | |
StringDuplicateBench.iodata 100: 1000000 1.47 µs/op | |
StringDuplicateBench.iodata to binary 100: 1000000 2.18 µs/op | |
StringDuplicateBench.strdup 1000: 1000000 2.20 µs/op | |
StringDuplicateBench.binary copy 1000: 500000 2.92 µs/op | |
StringDuplicateBench.List.duplicate 1000: 500000 7.39 µs/op | |
StringDuplicateBench.strdup 10000: 200000 7.66 µs/op | |
StringDuplicateBench.iodata square to binary 1000: 100000 10.61 µs/op | |
StringDuplicateBench.iodata 1000: 100000 13.06 µs/op | |
StringDuplicateBench.iodata to binary 1000: 100000 18.74 µs/op | |
StringDuplicateBench.binary copy 10000: 100000 29.19 µs/op | |
StringDuplicateBench.strdup 100000: 50000 65.22 µs/op | |
StringDuplicateBench.List.duplicate 10000: 20000 88.81 µs/op | |
StringDuplicateBench.iodata square to binary 10000: 10000 108.81 µs/op | |
StringDuplicateBench.iodata 10000: 10000 129.40 µs/op | |
StringDuplicateBench.iodata to binary 10000: 10000 188.71 µs/op | |
StringDuplicateBench.binary copy 100000: 10000 284.83 µs/op | |
StringDuplicateBench.List.duplicate 100000: 2000 811.83 µs/op | |
StringDuplicateBench.iodata square to binary 100000: 2000 970.95 µs/op | |
StringDuplicateBench.iodata 100000: 1000 1406.83 µs/op | |
StringDuplicateBench.iodata to binary 100000: 500 3579.47 µs/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment