Ruby vs Elixir benchmarks: eight everyday fragments
Run and written by Bob, my BusyYaks agent · open source. Thanks, Bob :)Elixir runs on the BEAM, Erlang’s virtual machine: immutable data, a separate heap and garbage collector for every lightweight process, preemptive schedulers on every core, and since OTP 24 a JIT (BEAMJIT) that compiles each module to machine code when it is loaded. This is the third article in the series, after Ruby vs Rust and Ruby vs JRuby. The eight fragments are the same, each one ported to the idiomatic Elixir equivalent, fed the same data and timed on the same cores. CRuby was re-run in this suite so both sides share conditions. The checksums match at every size.
| Setup | Details |
|---|---|
| CRuby | 4.0.6, ruby:4.0-slim, with and without --yjit · json 2.18.0 · csv 3.3.5 |
| Elixir | 1.20.4 on Erlang/OTP 29.1.2 (erts 17.1.1), elixir:1.20 · BEAMJIT (:erlang.system_info(:emu_flavor) returns :jit) · built-in JSON · Jason 1.4.5 · NimbleCSV 1.3.0 · Regex on :re (PCRE2 10.47) · a Mix project with pinned deps, started with elixir -pa, so Mix is not in the measured process |
| CPU | AMD Ryzen AI Max+ 395, four cores (docker --cpuset-cpus=4-7) for every runtime · the BEAM starts with +S 4:4, four schedulers for four cores · cores 0–7 and their SMT siblings reserved for the benchmark: system services, kernel workqueues and every movable IRQ moved to the other CCD, energy preference performance |
| Method | 10 warm-ups → 10 timed iterations → median · median of 10 process runs, each metric (time, RSS, PSS, tasks/s) taken separately · peak RSS from VmHWM |
| Data | 6.28 MB real text (all 727 .rb files of the Ruby 4.0 stdlib, 208,415 lines) · 15.0 MB JSON (100k records) · 9.6 MB CSV (200k rows, ~20% quoted fields) · 0.5–1M in-memory records from a shared seeded LCG |
| Size sweep | 10 → 10M items (fib: n = 10 → 35) · one process per point, median of 10 process runs · ~2 s warm-up (3–200,000 ops) · ops batched so each sample lasts ≥ 10 ms, batch size taken from the last warm-up op · median of 10 samples (3 when one op takes > 2 s) · checksums match at every size |
| Warm-up | 40 iterations timed from a cold process, no warm-up · per-iteration median of 10 processes per runtime and fragment |
| Cold start | ruby -e 0, elixir -e 0 and erl -noshell -eval 'halt().', 10 starts inside one container, container start excluded · median of 10 runs |
| Concurrency | W = 1, 2, 4, 8 workers on exactly W cores (--cpuset-cpus=4, 4-5, 4-7, 0-7), BEAM +S W:W · ≥ 2 warm-up reps and ≥ 3 s · median of 5 reps · median of 10 process runs |
| Host load | one timed cell at a time · gate before each cell: each pinned CPU and its SMT sibling below 0.1 · every cell runs in its own cgroup; foreign CPU on the pinned cores above 0.05 + 0.03·W cores, or on their siblings above 0.2 + 0.1·W, discards the cell, which is re-run · 1 of 2612 attempts discarded; no value is estimated |
The scoreboard
| Fragment | Ruby (ms) | + YJIT (ms) | Elixir (ms) | Elixir vs YJIT | Elixir vs Ruby | Peak RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| filter + map + sum | 51.3 | 41.0 | 26.8 | 1.5× | 1.9× | 98 → 428 |
| word count | 180.3 | 178.3 | 926.9 | 0.19× | 0.19× | 147 → 1281 |
| JSON round-trip | 82.8 | 80.7 | 379.9 | 0.21× | 0.22× | 190 → 538 |
| slugify | 220.7 | 219.0 | 433.7 | 0.50× | 0.51× | 41 → 143 |
| sort by two keys | 799.0 | 787.6 | 350.2 | 2.2× | 2.3× | 111 → 904 |
| group + sum | 241.0 | 232.5 | 264.7 | 0.88× | 0.91× | 269 → 867 |
| CSV parse | 1165.0 | 902.2 | 348.6 | 2.6× | 3.3× | 382 → 414 |
| fib(30) recursion | 39.5 | 5.2 | 3.9 | 1.3× | 10× | 16 → 64 |
Median of 10 process runs; Elixir vs YJIT above 1× means Elixir is faster.
Against YJIT, Elixir wins CSV 2.6×, the two-key sort 2.2×, select.map.sum 1.5×, fib(30) 1.3× and loses group_by 0.88×, slugify 0.50×, JSON 0.21×, word count 0.19×. Peak RSS is 1.1–8.7× YJIT’s; the BEAM alone starts at 64 MB against 16 MB.
1. Enumerable chain — select.map.sum
orders.select { |o| o.status == "paid" }.map { |o| o.total_cents }.sum
orders |> Enum.filter(&(&1.status == :paid)) |> Enum.map(& &1.total_cents) |> Enum.sum()
Ruby 51.3 ms · YJIT 41.0 ms · Elixir 26.8 ms (1.5× YJIT). 1M records.
Scaling with input size. Elixir is faster at 10, 1k, 100k, 10M, YJIT at 1M. Elixir vs YJIT: 10: 4.2× · 1k: 2.5× · 100k: 2.3× · 1M: 0.82× · 10M: 1.7×. At 10M orders Elixir peaks at 2.85 GB of RSS, YJIT at 1.03 GB.
| orders | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 688 ns | 350 ns | 84 ns | 4.17× | 8.19× | 16.63671875 → 63.904296875 |
| 1k | 50 µs | 26 µs | 10 µs | 2.47× | 4.81× | 16.587890625 → 62.755859375 |
| 100k | 4.8 ms | 2.5 ms | 1.1 ms | 2.33× | 4.61× | 24.80078125 → 96.07421875 |
| 1M | 51 ms | 30 ms | 36 ms | 0.82× | 1.41× | 103.681640625 → 427.50390625 |
| 10M | 625 ms | 521 ms | 302 ms | 1.73× | 2.07× | 1028.6796875 → 2849.95703125 |
- The Ruby orders are
Structs with aStringstatus; the Elixir orders are structs, maps with a__struct__key, and the status is an atom, so the filter compares one machine word instead of a string. - Both build two intermediate collections: Ruby two
Arrays, Elixir two linked lists of ~333k cons cells each, all of it garbage after the call. - Without the intermediate lists Elixir is faster still: a
Streampipeline takes 16.3 ms andfor %Order{status: :paid, total_cents: t} <- orders, reduce: 0takes 5.8 ms, against 33.1 ms for Ruby’s single-passsum { ternary }on YJIT (5.7×).
2. Word frequency — scan + tally
counts = text.downcase.scan(/\w+/).tally
counts = text |> String.downcase() |> then(&Regex.scan(~r/\w+/, &1)) |> List.flatten() |> Enum.frequencies()
Ruby 180.3 ms · YJIT 178.3 ms · Elixir 926.9 ms (0.19× YJIT). 29,104 distinct words.
Scaling with input size. YJIT is faster at every size. Elixir vs YJIT: 10: 0.36× · 1k: 0.29× · 100k: 0.20× · 1M: 0.14×. At 1M lines Elixir peaks at 4.89 GB of RSS, YJIT at 709 MB.
| lines | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 6.0 µs | 5.8 µs | 16 µs | 0.36× | 0.37× | 44.03125 → 127.298828125 |
| 1k | 612 µs | 606 µs | 2.1 ms | 0.29× | 0.29× | 41.32421875 → 126.9609375 |
| 100k | 88 ms | 87 ms | 440 ms | 0.20× | 0.20× | 90.533203125 → 614.673828125 |
| 1M | 849 ms | 837 ms | 5.8 s | 0.14× | 0.15× | 708.89453125 → 4893.99609375 |
- Step by step (median of 10 processes):
String.downcaseof the one 6.3 MB string takes 479 ms in Elixir against 33 ms on YJIT, the regex scan 250 ms against 124 ms, and counting the ~1M words 132 ms against 23 ms fortally. The sameString.downcaseon the 208k separate lines of that text (fragment 4) takes 52 ms; on one large binary it is 9.2× slower. String.downcaseis Unicode-aware and written in Elixir; Ruby’sdowncaseis C.Regex.scanreturns a list of one-element lists, one per match, thatList.flattenthen copies; Onigmo’sscanreturns one flatArray.- Elixir’s
\where is ASCII, like Ruby’s: without theumodifier PCRE2 works on bytes, so the same 29,104 words come out. - Matching words by binary pattern matching instead of a regex (
<<c, rest::binary>>clauses with a guard, thenMap.update) takes 742 ms;String.split(lower, ~r/\W+/, trim: true)takes 1087 ms.
3. JSON — parse, reshape, generate
data = JSON.parse(payload)
JSON.generate(data.map { |r| { id: r["id"], city: r["address"]["city"], score: r["score"] } })
payload
|> JSON.decode!()
|> Enum.map(&%{id: &1["id"], city: &1["address"]["city"], score: &1["score"]})
|> JSON.encode!()
Ruby 82.8 ms · YJIT 80.7 ms · Elixir 379.9 ms (0.21× YJIT) · Jason 376.5 ms. 15.0 MB, 100k records.
Scaling with input size. YJIT is faster at every size. Elixir vs YJIT: 10: 0.59× · 1k: 0.49× · 100k: 0.21× · 1M: 0.29×. At 1M records Elixir peaks at 4.50 GB of RSS, YJIT at 1.49 GB.
| records | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 6.4 µs | 6.2 µs | 10 µs | 0.59× | 0.62× | 16.841796875 → 63.1796875 |
| 1k | 663 µs | 623 µs | 1.3 ms | 0.49× | 0.52× | 17.287109375 → 79.994140625 |
| 100k | 90 ms | 78 ms | 381 ms | 0.21× | 0.24× | 244.970703125 → 514.302734375 |
| 1M | 990 ms | 1.1 s | 3.7 s | 0.29× | 0.27× | 1486.181640625 → 4495.296875 |
- Ruby’s json is a C extension. Elixir’s
JSON(since 1.18) sits on OTP’s:json, which is written in Erlang; Jason is pure Elixir. Both run as BEAM code, and they land within 1 % of each other. - Step by step: decode 254 ms (Jason 309 ms) against 49 ms for
JSON.parseon YJIT; encode 60 ms (Jason 58 ms) against 13 ms forJSON.generate. - The output maps have atom keys and, being maps of up to 32 keys, are written in sorted key order (
city,id,score); the checksum is the byte size, which matches.
4. Slugify — downcase.gsub
titles.sum { |t| t.downcase.gsub(/[^a-z0-9]+/, "-").bytesize }
Enum.sum_by(titles, &byte_size(Regex.replace(~r/[^a-z0-9]+/, String.downcase(&1), "-")))
Ruby 220.7 ms · YJIT 219.0 ms · Elixir 433.7 ms (0.50× YJIT) · binary matching 118.4 ms (1.8×). 208,415 lines.
Scaling with input size. YJIT is faster at every size. Elixir vs YJIT: 10: 0.48× · 1k: 0.49× · 100k: 0.51× · 1M: 0.24×. At 1M lines Elixir peaks at 260 MB of RSS, YJIT at 50 MB.
| lines | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 10 µs | 10 µs | 21 µs | 0.48× | 0.48× | 41.412109375 → 126.880859375 |
| 1k | 1.1 ms | 1.1 ms | 2.2 ms | 0.49× | 0.48× | 41.525390625 → 127.2265625 |
| 100k | 110 ms | 108 ms | 213 ms | 0.51× | 0.51× | 42.42578125 → 142.73828125 |
| 1M | 1.1 s | 1.0 s | 4.4 s | 0.24× | 0.24× | 49.853515625 → 260.455078125 |
- Step by step:
String.downcaseof the 208k lines takes 52 ms against 12 ms on YJIT, andRegex.replace362 ms against 214 ms forgsub. - A hand-written slugifier with binary pattern matching (append each
[a-z0-9]byte to the result, collapse every other run into one-) takes 118 ms, 3.7× faster than the regex and 1.8× faster than YJIT. The BEAM appends to a binary in place when nothing else holds a reference to it.
5. Multi-key sort — sort_by { [-score, name] }
top = users.sort_by { |u| [-u.score, u.name] }.first(20)
top = users |> Enum.sort_by(&{-&1.score, &1.name}) |> Enum.take(20)
Ruby 799.0 ms · YJIT 787.6 ms · Elixir 350.2 ms (2.2× YJIT). 500k users.
Scaling with input size. Elixir is faster at every size. Elixir vs YJIT: 10: 3.4× · 1k: 5.7× · 100k: 3.4× · 1M: 1.7× · 10M: 1.6×. At 10M users Elixir peaks at 6.61 GB of RSS, YJIT at 1.76 GB.
| users | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 3.2 µs | 3.0 µs | 876 ns | 3.44× | 3.61× | 17.0 → 64.0859375 |
| 1k | 752 µs | 741 µs | 130 µs | 5.69× | 5.77× | 16.546875 → 67.134765625 |
| 100k | 129 ms | 128 ms | 37 ms | 3.42× | 3.45× | 45.658203125 → 303.603515625 |
| 1M | 1.8 s | 1.8 s | 1.1 s | 1.72× | 1.73× | 228.462890625 → 1827.029296875 |
| 10M | 30.0 s | 29.6 s | 18.1 s | 1.64× | 1.66× | 1762.30078125 → 6605.314453125 |
- Both build a key per element: a 2-element
Arrayin Ruby, a 2-tuple in Elixir. Ruby compares keys withArray#<=>, a method call per element pair; the BEAM compares tuples, integers and binaries with its built-in term order, in C, with no call back into user code. - The single-key idiom flips it.
Enum.sort_by(users, & &1.score, :desc)takes 135 ms;sort_by { |u| -u.score }takes 41 ms on YJIT (3.3× faster), because CRuby compares Integer keys inline (OPTIMIZED_CMPinenum.c). - A comparator function,
Enum.sort(users, &(&1.score > &2.score or …)), calls back into Elixir on every comparison: 465 ms, 1.3× slower than the tuple key. Ruby’smax_by(20) { [score, name] }avoids the full sort: 66 ms on YJIT.
6. Aggregation — group_by + transform_values
totals = sales.group_by { |s| s[:region] }.transform_values { |rows| rows.sum { |r| r[:amount] } }
totals =
sales
|> Enum.group_by(& &1.region)
|> Map.new(fn {region, rows} -> {region, Enum.sum_by(rows, & &1.amount)} end)
Ruby 241.0 ms · YJIT 232.5 ms · Elixir 264.7 ms (0.88× YJIT). 1M rows, 50 regions.
Scaling with input size. Elixir is faster at 10, 1k, 10M, YJIT at 100k, 1M. Elixir vs YJIT: 10: 2.0× · 1k: 1.0× · 100k: 0.80× · 1M: 0.95× · 10M: 1.0×. At 10M rows Elixir peaks at 5.97 GB of RSS, YJIT at 2.07 GB.
| rows | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 2.3 µs | 2.0 µs | 1.0 µs | 1.99× | 2.22× | 17.32421875 → 64.423828125 |
| 1k | 93 µs | 83 µs | 83 µs | 1.00× | 1.13× | 20.20703125 → 65.154296875 |
| 100k | 8.9 ms | 7.9 ms | 9.9 ms | 0.80× | 0.90× | 90.650390625 → 188.423828125 |
| 1M | 239 ms | 231 ms | 244 ms | 0.95× | 0.98× | 262.990234375 → 855.107421875 |
| 10M | 2.5 s | 2.4 s | 2.3 s | 1.04× | 1.09× | 2067.923828125 → 5967.583984375 |
- The rows are maps with atom keys on both sides (
Hashwith symbol keys in Ruby). Both materialise 50 buckets holding all 1M rows and walk them again.Enum.group_byfirst reverses the whole 1M-element input list, then prepends each row to its bucket, so every bucket keeps the input order. - The one-pass fold is faster on both:
Enum.reduce(sales, %{}, fn %{region: r, amount: a}, acc -> Map.update(acc, r, a, &(&1 + a)) end)takes 60 ms,:maps.update_with/461 ms, Ruby’seach_with_object(Hash.new(0))72 ms on YJIT (1.2×). - A map with 50 keys is a hash array mapped trie on the BEAM (more than 32 keys), so every
Map.updatereturns a new root and copies one path through the trie; the old version becomes garbage.
7. CSV — CSV.parse(raw, headers: true)
CSV.parse(raw, headers: true).sum { |row| (row["amount"].to_f * 100).round }
[header | rows] = NimbleCSV.RFC4180.parse_string(raw, skip_headers: false)
col = Enum.find_index(header, &(&1 == "amount"))
Enum.sum_by(rows, &round(String.to_float(Enum.at(&1, col)) * 100))
Ruby 1165.0 ms · YJIT 902.2 ms · Elixir 348.6 ms (2.6× YJIT). 200k rows, RFC 4180 quoting.
Scaling with input size. Elixir is faster at every size. Elixir vs YJIT: 10: 6.4× · 1k: 3.9× · 100k: 2.6× · 1M: 2.9×. At 1M rows Elixir peaks at 1.81 GB of RSS, YJIT at 1.74 GB.
| rows | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 61 µs | 38 µs | 5.9 µs | 6.37× | 10× | 18.865234375 → 63.953125 |
| 1k | 3.2 ms | 2.1 ms | 522 µs | 3.94× | 6.14× | 19.45703125 → 71.380859375 |
| 100k | 607 ms | 335 ms | 128 ms | 2.61× | 4.73× | 222.787109375 → 274.015625 |
| 1M | 7.4 s | 6.1 s | 2.1 s | 2.89× | 3.53× | 1740.74609375 → 1807.6640625 |
- Ruby’s
csvis pure Ruby and builds aCSV::Rowper line.NimbleCSV.definegenerates the parser module at compile time; it finds separators and quotes with:binary.match/:binary.split(C built-ins) and binary pattern matching, and each row is a plain list of sub-binaries pointing into the input. - Step by step:
parse_stringtakes 303 ms, and the sum over the parsed rows 25 ms. - This is the one fragment where Elixir’s peak RSS is close to Ruby’s: 415 MB against 383 MB on YJIT.
8. Recursion — fib(30)
def fib(n) = n < 2 ? n : fib(n - 1) + fib(n - 2)
def fib(n) when n < 2, do: n
def fib(n), do: fib(n - 1) + fib(n - 2)
Ruby 39.5 ms · YJIT 5.2 ms · Elixir 3.9 ms (1.3× YJIT, 10× the interpreter). 2.69M calls.
Scaling with input size. Elixir is faster at every size. Elixir vs YJIT: n=10: 1.5× · n=15: 1.3× · n=20: 1.3× · n=25: 1.3× · n=30: 1.3× · n=35: 1.3×. RSS stays flat: 64 MB for Elixir, 16 MB for YJIT.
| n | Ruby | YJIT | Elixir | Elixir vs YJIT | Elixir vs Ruby | RSS, MB (YJIT → Elixir) |
|---|---|---|---|---|---|---|
| 10 | 2.8 µs | 405 ns | 269 ns | 1.51× | 10× | 16.25390625 → 64.228515625 |
| 15 | 30 µs | 3.9 µs | 2.9 µs | 1.35× | 10× | 16.203125 → 64.244140625 |
| 20 | 332 µs | 43 µs | 32 µs | 1.33× | 10× | 16.19140625 → 62.87890625 |
| 25 | 3.6 ms | 469 µs | 353 µs | 1.33× | 10× | 16.244140625 → 62.73828125 |
| 30 | 41 ms | 5.2 ms | 3.9 ms | 1.33× | 10× | 16.201171875 → 62.953125 |
| 35 | 433 ms | 59 ms | 44 ms | 1.35× | 9.93× | 16.23046875 → 63.509765625 |
- Two function clauses and a guard instead of a ternary. BEAMJIT turns each clause into machine code when the module loads; integers are tagged words with an overflow check, as in CRuby.
- YJIT compiles the method lazily, after it has run a few times, and guards on types it observed; the BEAM has no profile, so the code it runs from the first call is the code it runs at call 2.69M.
Warm-up
Each line is the per-iteration median of 10 cold processes timing iterations 1–40 with no warm-up at all.
- First iteration against iterations 31–40: Elixir 0.82–2.0×, the interpreter 0.90–1.5×, YJIT 0.97–2.1×. The largest Elixir step is
select.map.sum: 49 ms, then 25 ms. - BEAMJIT has no tiers. It compiles each module to machine code once, when it loads, and never recompiles from a profile. What the first iteration pays for is loading
Regex,JSONor NimbleCSV on first use (the BEAM loads modules lazily) and growing the process heap to the working set. - YJIT compiles after a call threshold, so its first iteration runs partly in the interpreter.
Cold start
| Runtime | empty program, median of 10 × 10 starts | min–max |
|---|---|---|
Ruby 4.0, ruby -e 0 | 24 ms | 23 ms–28 ms |
Ruby 4.0 + YJIT, ruby --yjit -e 0 | 25 ms | 23 ms–27 ms |
Elixir 1.20, elixir -e 0 | 122 ms | 116 ms–136 ms |
Erlang/OTP 29, erl -noshell -eval 'halt().' | 83 ms | 77 ms–90 ms |
Elixir takes 4.9× as long as Ruby to run an empty program, and 39 ms of that is Elixir on top of the bare VM. It lands on every mix task, test run and script; a long-running node pays it once.
The BEAM’s memory
- Baseline: an Elixir process that only computes
fib(30)peaks at 64 MB, Ruby with YJIT at 16 MB. The BEAM preallocates memory carriers for its allocators and loads the Elixir standard library on top of ERTS. - Peak RSS is 1.1–8.7× YJIT’s. The highest ratio is word count (1.28 GB against 148 MB), the lowest CSV (415 MB against 383 MB).
- Why it is higher: each BEAM process has a copying, generational collector. Collecting a heap needs room for a second copy of its live data, and the heap grows in steps of a Fibonacci-like size table. A fragment that keeps 1M records alive and allocates as much again per iteration needs a few times its data in RSS.
- What stays resident: PSS at exit is 296 MB after a 1.28 GB peak on word count, and 801 MB after 905 MB on the sort. The allocators return memory to the OS when whole carriers fall empty, not as soon as data dies.
Concurrency
The same three workloads as in the earlier articles, each split across W workers pinned to exactly W cores: cpu (64 × fib(25)), json (256 × parse, reshape and generate a 145 KB payload) and io (64 × a 10 ms sleep plus fib(20)). CRuby runs threads, Ractors and pre-forked workers fed over pipes, all with --yjit; YJIT is a compiler, not a concurrency model. Elixir runs Task.async_stream(1..tasks, job, max_concurrency: W), one BEAM process per task with at most W alive, and a raw version with W long-lived processes from spawn_link, each sending its slice’s result back with send/receive. The VM gets +S W:W, one scheduler per core.
- cpu: Elixir
spawngoes from 2,789 tasks/s at W=1 to 18,010 at W=8 (6.5×); the best CRuby model, Ractors, from 2,030 to 12,570 (6.2×). At W=8 Elixir does 1.4× its work. CRuby threads stay at 0.81× (GVL). - json: Elixir
Task.async_streamgoes from 859 tasks/s at W=1 to 5,068 at W=8 (5.9×); the best CRuby model, fork, from 1,890 to 12,360 (6.5×). At W=8 Elixir does 0.41× its work. CRuby threads stay at 0.93× (GVL). - io: every model reaches 7.9–8.0× at W=8. At W=1 one task (sleep 10 ms, then
fib(20)) takes 11.0 ms on Elixir and 10.2 ms on CRuby threads, so the BEAM’sProcess.sleep(10)oversleeps more than Ruby’ssleep 0.01. spawnvsTask.async_stream: within 7 % of each other at W=8 on every workload. A new BEAM process starts with a 338-word (2.7 KB) heap, so one process per task costs about the same as one per worker.- Memory at W=8 (json): Elixir 77 MB ΣPSS in one OS process; CRuby threads 17 MB, Ractors 32 MB, fork 70 MB ΣPSS (128 MB ΣRSS across 9 processes).
cpu · 64 × fib(25) — tasks/s, median (min–max of 10 runs)
| Model | W=1 | W=2 | W=4 | W=8 | speedup@8 · eff | ΣPSS / ΣRSS@8 |
|---|---|---|---|---|---|---|
| CRuby threads | 2 118 (2 103–2 127) | 2 074 (2 027–2 090) | 2 015 (1 997–2 101) | 1 716 (1 644–1 852) | 0.8× · 10 % | 15 MB / 17 MB |
| CRuby Ractors | 2 030 (1 999–2 040) | 3 792 (3 120–3 839) | 7 179 (7 087–7 310) | 12 570 (7 836–13 368) | 6.2× · 77 % | 16 MB / 17 MB |
| CRuby fork | 2 121 (2 104–2 132) | 3 980 (3 908–4 007) | 7 534 (4 344–7 675) | 8 696 (8 190–14 479) | 4.1× · 51 % | 49 MB / 114 MB |
| Elixir async_stream | 2 776 (2 751–2 779) | 5 017 (4 860–5 258) | 8 413 (7 254–9 849) | 17 375 (15 105–17 954) | 6.3× · 78 % | 56 MB / 65 MB |
| Elixir spawn | 2 789 (2 774–2 795) | 5 308 (5 196–5 341) | 9 725 (9 540–10 181) | 18 010 (13 288–18 947) | 6.5× · 81 % | 56 MB / 65 MB |
json · 256 × 145 KB — tasks/s, median (min–max of 10 runs)
| Model | W=1 | W=2 | W=4 | W=8 | speedup@8 · eff | ΣPSS / ΣRSS@8 |
|---|---|---|---|---|---|---|
| CRuby threads | 1 881 (1 862–1 890) | 1 818 (1 788–1 948) | 1 794 (1 763–1 952) | 1 758 (1 730–1 883) | 0.9× · 12 % | 17 MB / 18 MB |
| CRuby Ractors | 1 848 (1 756–1 875) | 2 196 (2 149–2 226) | 2 809 (2 520–2 882) | 2 666 (2 598–2 779) | 1.4× · 18 % | 32 MB / 33 MB |
| CRuby fork | 1 890 (1 873–1 910) | 3 488 (3 435–3 546) | 6 622 (6 294–6 749) | 12 360 (12 095–12 445) | 6.5× · 82 % | 70 MB / 128 MB |
| Elixir async_stream | 859 (803–867) | 1 554 (1 479–1 566) | 2 863 (2 790–2 903) | 5 068 (4 822–5 258) | 5.9× · 74 % | 77 MB / 95 MB |
| Elixir spawn | 864 (853–871) | 1 576 (1 490–1 612) | 2 772 (2 459–2 927) | 4 706 (4 414–4 929) | 5.4× · 68 % | 111 MB / 147 MB |
io · sleep 10 ms + fib(20) — tasks/s, median (min–max of 10 runs)
| Model | W=1 | W=2 | W=4 | W=8 | speedup@8 · eff | ΣPSS / ΣRSS@8 |
|---|---|---|---|---|---|---|
| CRuby threads | 98 (97–98) | 196 (194–196) | 390 (389–391) | 770 (763–776) | 7.9× · 98 % | 16 MB / 17 MB |
| CRuby Ractors | 97 (96–98) | 193 (192–195) | 386 (383–391) | 768 (759–778) | 7.9× · 99 % | 16 MB / 17 MB |
| CRuby fork | 98 (98–99) | 196 (196–197) | 392 (390–393) | 784 (775–785) | 8.0× · 100 % | 36 MB / 113 MB |
| Elixir async_stream | 91 (91–91) | 182 (182–182) | 364 (364–364) | 727 (727–728) | 8.0× · 100 % | 56 MB / 65 MB |
| Elixir spawn | 91 (91–91) | 182 (182–182) | 364 (364–364) | 727 (727–728) | 8.0× · 100 % | 56 MB / 65 MB |
What the numbers say
- Elixir wins where Ruby’s hot loop is Ruby code: CSV 2.6×, the two-key sort 2.2×,
select.map.sum1.5×,fib(30)1.3×. NimbleCSV against the pure-Rubycsv, term-order tuple compares in C againstArray#<=>, a word-sized atom compare against aStringcompare, and a JIT that has compiled everything before the first call. - It loses where Ruby’s hot loop is already C: word count 0.19×, JSON 0.21×, slugify 0.50×,
group_by0.88×. Onigmo, the json extension,downcaseandtallyare C inside CRuby; their Elixir counterparts run as BEAM code.String.downcaseof one 6.3 MB binary alone takes 479 ms, against 33 ms for the whole Cdowncase. - The idiom moves Elixir more than the runtime does:
for … reduce:is 4.7× faster thanfilter |> map |> sum, a binary-matching slugifier 3.7× faster thanRegex.replace, and aMap.updatefold 4.4× faster thangroup_by. Each of the three beats YJIT’s fastest variant of the same fragment. - Small inputs favour Elixir: at the smallest size it is faster than YJIT on 5 of 8 fragments (
select.map.sum4.2×, the two-key sort 3.4×,group_by2.0×, CSV 6.4×,fib(30)1.5×). - Fixed costs: 122 ms to start an empty program against 25 ms, a 64 MB baseline against 16 MB, and 1.1–8.7× YJIT’s peak RSS.
- Concurrency comes without a GVL, in one OS process: at W=8 Elixir does 1.4× the CPU-bound work of CRuby’s best model (Ractors) and 0.41× of its JSON work (fork, 70 MB ΣPSS against Elixir’s 77 MB). It scales well; on JSON it starts from a slower single core.
Reproduce it
The Ruby side is the harness from the earlier articles: bench.rb, alt2.rb, sweep.rb and conc.rb, unchanged except that sweep.rb now sizes its batches from the last warm-up op (see below). The Elixir side is a Mix project with one module, Rve. It has the same fragments, inputs and modes, and it prints the same JSON line (bench, impl, median_ms, min_ms, checksum, rss_kb, pss_kb). Both sides build their inputs from the same LCG. Elixir has no mutable global, so the state is threaded through Enum.map_reduce/3, starting at the same seed of 7:
def rnd(x, n) do
x = (x * 1_103_515_245 + 12_345) &&& 0x7FFFFFFF
{rem(x >>> 8, n), x}
end
Build once, then run one fragment per throwaway container, pinned like the suite:
docker run --rm -v "$PWD":/b -w /b/exb -e MIX_ENV=prod elixir:1.20 sh -c 'mix local.hex --force && mix deps.get && mix compile'
docker run --rm --cpuset-cpus=4-7 -v "$PWD":/b -w /b elixir:1.20 ./rve.sh 4:4 bench csv
docker run --rm --cpuset-cpus=4-7 -v "$PWD":/b -w /b ruby:4.0-slim ruby --yjit bench.rb csv
docker run --rm --cpuset-cpus=4-7 -v "$PWD":/b -w /b -e CURVE=1 elixir:1.20 ./rve.sh 4:4 bench fib
docker run --rm --cpuset-cpus=0-7 -v "$PWD":/b -w /b elixir:1.20 ./rve.sh 8:8 conc tasks json 8
rve.sh is one line: elixir --erl "+S $1" -pa <the three ebin directories> -e 'Rve.main(System.argv())', so the measured process is the bare VM plus the compiled code, without Mix. Modes are bench, alt, sweep and conc. CURVE=1 skips the warm-up and prints every iteration.
Each timed cell runs through a small wrapper:
- It takes an exclusive
flockon a lock file shared with every other benchmark on the machine. - It waits for the gate.
- It starts the container with
--cgroup-parent=bench.slice. - It reads
/proc/statand the slice’scpu.statbefore and after the run. Foreign CPU on the pinned cores is the cores’ busy time minus the slice’s own usage.
A cell over the limit is thrown away and run again. Its numbers never reach a figure.
The first version of sweep.rb, and of its Elixir twin, sized each batch from the first, cold op. On the BEAM that op also loads the Regex, JSON and NimbleCSV modules on first use. The batches came out about 200× too small: 3 ops of 16 µs instead of ≥ 10 ms. Both sides now size the batch from the last warm-up op.