In my benchmarks it’s about 3x faster than the formatter implementation for 7-digit strings (8x for 1kb strings but that’s probably not a realistic use-case). A fun little challenge but I know people aren’t a fan of the bitstring syntax
Name ips average deviation median 99th %
adamu 5.46 M 183.02 ns ±8688.03% 125 ns 250 ns
formatter 1.97 M 508.32 ns ±3909.15% 334 ns 542 ns
Comparison:
adamu 5.46 M
formatter 1.97 M - 2.78x slower +325.30 ns
Memory usage statistics:
Name Memory usage
adamu 0.40 KB
formatter 1.89 KB - 4.75x memory usage +1.49 KB
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 16 GB
Elixir 1.15.4
Erlang 26.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 500 ms
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 15 s
nice! I see a way to fairly easily avoid “-_654_321”. (I know digits are supposed to be just digits…)
In late '69 or early '70 I sped up a snobol program (the “compositor” on the SDS 940 at NOAA in Boulder) that formatted text for publication. The function to add spaces between words to make the line a give width was really slow. Among other things, it would call reverse() twice on every other line so the white space looked balanced. I used a bit of arithmetic to do the job without creating temporary strings of the line of text. (The garbage collector was really slow.)