Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Elixir benchmark #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions elixir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
elixir-*.tar

13 changes: 13 additions & 0 deletions elixir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Elixir

Running it once will compile the app.

## Compile
```sh
MIX_ENV=prod mix run lib/main.exs
```

## Execute
```sh
mix run lib/main.exs
```
38 changes: 38 additions & 0 deletions elixir/lib/bulk_ops.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule BulkOps do
defmodule SplitResult do
defstruct [:lower, :equal, :greater]
end

def merge(nil, greater), do: greater
def merge(lower, nil), do: lower

def merge(lower, greater) do
if lower.y < greater.y do
%{lower | right: merge(lower.right, greater)}
else
%{greater | left: merge(lower, greater.left)}
end
end

def split_binary(nil, _value), do: {nil, nil}

def split_binary(orig, value) do
if orig.x < value do
{first, second} = split_binary(orig.right, value)
{%{orig | right: first}, second}
else
{first, second} = split_binary(orig.left, value)
{first, %{orig | left: second}}
end
end

def merge3(lower, equal, greater) do
merge(merge(lower, equal), greater)
end

def split(orig, value) do
{lower, equal_greater} = split_binary(orig, value)
{equal, greater} = split_binary(equal_greater, value + 1)
%SplitResult{lower: lower, equal: equal, greater: greater}
end
end
23 changes: 23 additions & 0 deletions elixir/lib/main.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{_cur, _root, res} =
Enum.reduce(1..1_000_000, {5, nil, 0}, fn i, acc ->
{cur, root, res} = acc
a = rem(i, 3)
cur = rem(cur * 57 + 43, 10007)

case a do
0 ->
root = Treap.insert(root, cur)
{cur, root, res}

1 ->
root = Treap.erase(root, cur)
{cur, root, res}

2 ->
{root, present} = Treap.has_value?(root, cur)
res = if present, do: res + 1, else: res
{cur, root, res}
end
end)

IO.puts(res)
27 changes: 27 additions & 0 deletions elixir/lib/treap.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Treap do
defmodule TreapNode do
defstruct [:x, :y, :left, :right]
end

def has_value?(root, x) do
splitted = BulkOps.split(root, x)
res = splitted.equal != nil
root = BulkOps.merge3(splitted.lower, splitted.equal, splitted.greater)
{root, res}
end

def insert(root, x) do
splitted = BulkOps.split(root, x)

if splitted.equal == nil do
BulkOps.merge3(splitted.lower, %TreapNode{x: x, y: :rand.uniform()}, splitted.greater)
else
BulkOps.merge3(splitted.lower, splitted.equal, splitted.greater)
end
end

def erase(root, x) do
splitted = BulkOps.split(root, x)
BulkOps.merge(splitted.lower, splitted.greater)
end
end
26 changes: 26 additions & 0 deletions elixir/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Elixir.MixProject do
use Mix.Project

def project do
[
app: :elixir,
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end