Created
August 6, 2018 06:51
-
-
Save angelikatyborska/8e77a2a18831d52f878b85ac68e17d0a to your computer and use it in GitHub Desktop.
Performance test to two different approaches to Exercism's Word Count in Elixir
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
defmodule Words do | |
def count_with_downcase_first(sentence) do | |
sentence | |
|> downcase_and_split | |
|> count_words | |
end | |
def count_with_split_first(sentence) do | |
sentence | |
|> split_and_downcase | |
|> count_words | |
end | |
defp downcase_and_split(sentence) do | |
word_delimiters = ~r/[^\p{L}0-9-]+/u | |
sentence | |
|> String.downcase() | |
|> String.split(word_delimiters, trim: true) | |
end | |
defp split_and_downcase(sentence) do | |
word_delimiters = ~r/[^\p{L}0-9-]+/u | |
sentence | |
|> String.split(word_delimiters, trim: true) | |
|> Enum.map(&String.downcase/1) | |
end | |
defp count_words(word_list) do | |
word_list | |
|> Enum.reduce(%{}, &increment/2) | |
end | |
defp increment(word, map) do | |
Map.update(map, word, 1, &(&1 + 1)) | |
end | |
end | |
n = 10000 | |
input = String.duplicate("Is Elixir awesome? Yes, Elixir is awesome!", n) | |
Benchee.run(%{ | |
"downcase first" => fn -> Words.count_with_downcase_first(input) end, | |
"split first" => fn -> Words.count_with_split_first(input) end | |
}, time: 10, memory_time: 2) | |
# Output: | |
# | |
# Operating System: macOS" | |
# CPU Information: Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz | |
# Number of Available Cores: 4 | |
# Available memory: 8 GB | |
# Elixir 1.7.1 | |
# Erlang 21.0.4 | |
# | |
# Benchmark suite executing with the following configuration: | |
# warmup: 2 s | |
# time: 10 s | |
# memory time: 2 s | |
# parallel: 1 | |
# inputs: none specified | |
# Estimated total run time: 28 s | |
# | |
# | |
# Benchmarking downcase first... | |
# Benchmarking split first... | |
# | |
# Name ips average deviation median 99th % | |
# downcase first 0.0349 28.68 s ±0.00% 28.68 s 28.68 s | |
# split first 0.0340 29.41 s ±0.00% 29.41 s 29.41 s | |
# | |
# Comparison: | |
# downcase first 0.0349 | |
# split first 0.0340 - 1.03x slower | |
# | |
# Memory usage statistics: | |
# | |
# Name Memory usage | |
# downcase first 43.61 MB | |
# split first 47.68 MB - 1.09x memory usage | |
# | |
# **All measurements for memory usage were the same** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment