Created
June 21, 2021 03:55
-
-
Save adamu/911b3754736f1c584e45222ba9a4c107 to your computer and use it in GitHub Desktop.
Benchmarking functions to count codepoints in strings
This file contains hidden or 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
| #! /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 | |
| ) |
This file contains hidden or 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
| 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