Skip to content

Instantly share code, notes, and snippets.

@cliffom
Created December 14, 2017 19:51
Show Gist options
  • Save cliffom/f4c08177fe7fbca3327217f2b6b41d9c to your computer and use it in GitHub Desktop.
Save cliffom/f4c08177fe7fbca3327217f2b6b41d9c to your computer and use it in GitHub Desktop.
defmodule HashData do
def open_file do
File.read! "rand.txt"
end
def hash_contents(n) do
open_file()
|> hash_contents(n)
end
def hash_contents(hash, n) when n < 1 do
hash
|> Base.encode16
|> String.downcase
end
def hash_contents(hash, n) do
:crypto.hash(:sha256, hash)
|> hash_contents(n - 1)
end
end
defmodule Benchmark do
def measure(function) do
function
|> :timer.tc
|> elem(0)
|> Kernel./(1_000_000)
end
end
@cliffom
Copy link
Author

cliffom commented Dec 15, 2017

Bench with:
Benchmark.measure(fn -> HashData.hash_contents(100_000_000) end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment