The measured cost of both ways per call
The two previous pages reach the same function, and this page says what each way costs per call. The comparison is fair by construction: one request line, one answer line, the same core, the same machine, the same Python interpreter, and five rounds of each so that a single slow sample cannot decide the result.
The answer is not close. A call through subprocess costs milliseconds, because it starts a process; a call through ctypes costs microseconds, because it is a function call. The interesting part is not the ratio but the sizes: three milliseconds is the price of correctness at a boundary, and thirteen microseconds is what remains when the protocol is inside one address space.
What is being timed
One request, PRICE coffee 3, which takes the discount branch of the core. Each round calls the way a fixed number of times and divides the elapsed wall clock by that number, so every sample is a per-call time in microseconds. The subprocess way runs 20 calls per round because each call is milliseconds; the ctypes way runs 2000 because each call is microseconds. Five rounds each.
What is inside each measurement is worth naming. The subprocess sample contains the fork, the exec, the loading of a 2.6 MB binary, the startup of the Haskell runtime, the read, the answer and the wait for the exit. The ctypes sample contains a create_string_buffer, the encode of the request, the call, the copy of the answer and the decode. Both include the protocol work, which is what a caller actually pays.
The benchmark
One function measures and one prints. The assertion at the start is the check that the two ways still agree before any number is printed, so a broken build cannot produce a fast wrong answer:
"""Per-call cost of the two ways to reach the core.""" import statistics import time from ctypes_client import ask from protocol import run_core LINE = "PRICE coffee 3" def measure(function, repeats: int, rounds: int = 5) -> list[float]: """Per-call microseconds for each round of `repeats` calls.""" samples = [] for _ in range(rounds): start = time.perf_counter() for _ in range(repeats): function(LINE) samples.append((time.perf_counter() - start) / repeats * 1_000_000) return samples def report(name: str, samples: list[float]) -> None: values = " ".join(f"{sample:8.2f}" for sample in samples) print(f"{name:22s}{values} min {min(samples):7.2f} median {statistics.median(samples):7.2f}") if __name__ == "__main__": assert run_core(LINE) == ask(LINE) report("subprocess per call", measure(run_core, 20)) report("ctypes, one process", measure(ask, 2000))
python3 bench.py
subprocess per call 2883.55 2810.79 2868.43 2768.14 2762.83 min 2762.83 median 2810.79
ctypes, one process 14.95 12.72 12.94 12.56 12.50 min 12.50 median 12.72
--- second run ---
subprocess per call 2791.43 2759.67 2777.17 2761.76 2796.08 min 2759.67 median 2777.17
ctypes, one process 15.42 13.08 12.92 13.28 12.73 min 12.73 median 13.08
status 0
The numbers on this machine
Two runs of the benchmark, a minute apart, so that the reader can see which figures move between runs and which do not. The per-call times are in microseconds.
| Run | Way | The five rounds, µs per call | Min | Median |
|---|---|---|---|---|
| 1 | subprocess per call | 2883.55, 2810.79, 2868.43, 2768.14, 2762.83 | 2762.83 | 2810.79 |
| 1 | ctypes, one process | 14.95, 12.72, 12.94, 12.56, 12.50 | 12.50 | 12.72 |
| 2 | subprocess per call | 2791.43, 2759.67, 2777.17, 2761.76, 2796.08 | 2759.67 | 2777.17 |
| 2 | ctypes, one process | 15.42, 13.08, 12.92, 13.28, 12.73 | 12.73 | 13.08 |
The machine and the settings behind the table:
- 4 CPUs (nproc 4), Alpine Linux 3.24.3, x86_64, in a Docker container.
- GHC 9.10.3: the executable built with -Wall -O1, the library with -Wall -O1 -shared -dynamic -fPIC and the dynamic RTS; no +RTS flags, so the default GC settings.
- Python 3.14.10 from the image, no virtual environment and no third-party package.
- The container is shared, so a round can be disturbed by another process; the minimum is the number to trust, and the medians of the two runs agree to within three percent.
The median of the second run is the number to quote: 2777 microseconds for a process per call and 13.08 for a library call, a factor of 212. The subprocess figures spread by about four percent within a run, because the process start depends on the machine's load; the ctypes figures spread by less, because a library call in a warm process depends on almost nothing.
What the factor is made of
Nearly all of the three milliseconds is the process. A fork and an exec of a dynamically linked binary is about a millisecond on this machine, the dynamic loader maps the Haskell libraries and relocates them, and hs_init allocates the runtime's heap and starts its threads. The protocol work is a microsecond of it, if that.
The thirteen microseconds of the library call are the boundary itself: the Python call machinery, the buffer allocation, the encode and decode of one short line, and the Haskell function. A pure ctypes call with no string is a fraction of a microsecond; the line protocol and its two encodings are most of the rest. A binary function that took two int64 values would cross the boundary in well under a microsecond, and that is the number the Rust and C extensions in other courses are compared on.
The lesson generalises: the cost of a boundary is the cost of what crosses it plus a fixed toll. A process boundary has a toll of milliseconds; a library boundary has a toll of microseconds and a risk that a process boundary does not have.
Which one to use
A request rate of a hundred per second is a hundred process starts per second, which is 0.28 seconds of CPU per second of wall clock in the table above. That is affordable, and it buys a wall between the two languages: a crash in the core is a 500 for one request, not a dead service. At a thousand per second the same arithmetic is no longer affordable, and the options are a long-lived child with pipes, a pool of them, or the library.
The library is the right answer when the call is frequent and the values are simple, and it is the wrong answer when the core owns state that has to survive a crash, or when the two sides are built by different toolchains that will drift. The process is the right answer when the boundary has to be a contract that either side can break without taking the other down.
The next page tests the Python layer, including the service, and is the part of the set that a team would keep after the first week. The Haskell lesson on measuring is measuring performance.
The measurements were taken with GHC 9.10.3, Python 3.14.10 and the tools of the image on a 4-CPU Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev