Skip to content

Instantly share code, notes, and snippets.

@adamu
Created June 21, 2021 03:55
Show Gist options
  • Select an option

  • Save adamu/911b3754736f1c584e45222ba9a4c107 to your computer and use it in GitHub Desktop.

Select an option

Save adamu/911b3754736f1c584e45222ba9a4c107 to your computer and use it in GitHub Desktop.
Benchmarking functions to count codepoints in strings
#! /usr/bin/env elixir
Mix.install([:benchee])
string = for i <- 0x1F600..0x1F64F, into: "", do: <<i::utf8>>
codepoints = fn -> string |> String.codepoints() |> length end
charlist = fn -> string |> String.to_charlist() |> length end
comprehension = fn -> for <<_::utf8 <- string>>, reduce: 0, do: (count -> count + 1) end
Benchee.run(
%{
"codepoints" => codepoints,
"charlist" => charlist,
"comprehension" => comprehension
},
memory_time: 5
)
Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz
Number of Available Cores: 4
Available memory: 24 GB
Elixir 1.12.1
Erlang 24.0.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 5 s
parallel: 1
inputs: none specified
Estimated total run time: 36 s
Benchmarking charlist...
Benchmarking codepoints...
Benchmarking comprehension...
Name ips average deviation median 99th %
charlist 743.00 K 1.35 μs ±1522.82% 1 μs 2 μs
comprehension 533.15 K 1.88 μs ±1094.83% 2 μs 3 μs
codepoints 489.06 K 2.04 μs ±1086.91% 2 μs 3 μs
Comparison:
charlist 743.00 K
comprehension 533.15 K - 1.39x slower +0.53 μs
codepoints 489.06 K - 1.52x slower +0.70 μs
Memory usage statistics:
Name Memory usage
charlist 1.25 KB
comprehension 6.45 KB - 5.16x memory usage +5.20 KB
codepoints 3.16 KB - 2.53x memory usage +1.91 KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment