Three ways, timed
Three ways to call one function, and one question: what does each cost? The client below does the three in one process, so the call is the same line through the same core, and the only difference is the transport. Each way runs five series of twenty calls, and the series are averaged into a per-call time before the minimum and the median are taken.
The client
by_process runs the CLI for one line, Daemon keeps one process alive, and by_api posts to the server; measure times twenty calls and divides:
#!/usr/bin/env python3
"""Three ways to call the C# pricing core, and a timing of each."""
import json
import statistics
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
CLI = ROOT / "src" / "Cli" / "bin" / "Release" / "net10.0" / "Cli.dll"
API = "http://127.0.0.1:5099/price"
LINE = "sku=pen qty=2 unit=1.50"
def by_process(line):
result = subprocess.run(
["dotnet", str(CLI), line],
capture_output=True,
text=True,
check=False,
timeout=10,
)
return result.stdout.strip()
class Daemon:
def __init__(self):
self.process = subprocess.Popen(
["dotnet", str(CLI), "--daemon"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
bufsize=1,
)
def price(self, line):
self.process.stdin.write(line + "\n")
self.process.stdin.flush()
return self.process.stdout.readline().strip()
def close(self):
self.process.stdin.close()
self.process.wait(timeout=10)
def by_api(line):
request = urllib.request.Request(API, data=line.encode(), method="POST")
with urllib.request.urlopen(request) as response:
return response.read().decode()
def measure(name, call, runs, calls):
times = []
for _ in range(runs):
start = time.perf_counter()
for _ in range(calls):
call(LINE)
times.append((time.perf_counter() - start) / calls * 1000)
ordered = sorted(times)
print(
f"{name}: min {ordered[0]:.3f} ms median {ordered[len(ordered) // 2]:.3f} ms "
f"runs " + " ".join(f"{time:.3f}" for time in times)
)
def demo():
print("process", by_process(LINE))
daemon = Daemon()
try:
print("daemon ", daemon.price(LINE))
print("daemon ", daemon.price("sku=paper qty=10 unit=0.25"))
finally:
daemon.close()
print("api ", by_api(LINE))
def timing(runs, calls):
daemon = Daemon()
try:
measure("process", by_process, runs, calls)
measure("daemon ", daemon.price, runs, calls)
measure("api ", by_api, runs, calls)
finally:
daemon.close()
if __name__ == "__main__":
command = sys.argv[1] if len(sys.argv) > 1 else "demo"
if command == "timing":
timing(int(sys.argv[2]) if len(sys.argv) > 2 else 5, int(sys.argv[3]) if len(sys.argv) > 3 else 20)
else:
demo()
The numbers
The API server was started before the measurement and the daemon was started by the client itself; both were warm, and nothing else of this project was running. Five series of twenty calls, per call, in milliseconds:
| Way | Series (ms per call) | Min | Median |
|---|---|---|---|
| a new process per call | 84.539, 110.269, 84.313, 85.309, 97.044 | 84.313 | 85.309 |
| the daemon on stdin and stdout | 1.403, 0.049, 0.047, 0.046, 0.047 | 0.046 | 0.047 |
| the minimal API over HTTP | 5.962, 2.533, 2.101, 2.271, 2.090 | 2.090 | 2.271 |
The fourth way: a native library
The NativeAOT page of this group compiles the same core to a shared library and calls it from Python ctypes. That run cannot happen in this container: its publish needs a C toolchain, which is why the page of that way says so. On the host of this container, with apk add --no-cache clang build-base zlib-dev, the same sources were published and called, five runs of twenty thousand calls each, in microseconds per call:
| Way | Series (µs per call) | Min | Median |
|---|---|---|---|
| the native library through ctypes | 3.42, 3.49, 3.50, 3.52, 4.78 | 3.42 | 3.50 |
That series comes from a different run from the three above: it was measured on the host, outside the default container, with the toolchain installed and a different number of calls per series. The pages quote it as the fourth number it is and do not divide it by a number from another run; what the two groups show together is that the native call is in microseconds and the transports of this container are in milliseconds or tens of milliseconds.
The machine and the versions of the three numbers measured here:
| Item | Value |
|---|---|
| CPU | Intel Core i3-6006U at 2.00 GHz, 4 processors, x86_64 |
| System | Alpine Linux 3.24.3 in a Docker container |
| C# | .NET SDK 10.0.303, runtime 10.0.15, Release builds, dotnet Cli.dll and dotnet Api.dll |
| Python | 3.14.10, python3 python/client.py timing 5 20 |
| Calls | five series of twenty calls per way, per-call averages, min and median |
How to read them
- A process per call is two orders of magnitude slower, and the cost is the start of the runtime: the C# vs Java group measured about 71 milliseconds for a hello program on this machine. The core itself is a split and two multiplications.
- The daemon is the fastest of the three by far: the line goes into a pipe and the answer comes back, with no protocol, no connection and no parse. The first series was 1.403 milliseconds, which is the cost of the first call after the process starts; the four after it are 0.046 to 0.049.
- The API is 48 times slower than the daemon and 38 times faster than a new process. That is the price of HTTP, and it buys a caller that can be on another machine, a browser, a load balancer and a status code.
- The choice is not the fastest number. A tool that runs one request per invocation should start a process per request and not run a daemon; a service that receives a thousand requests a second should not start a process, and its core should be in-process rather than behind any of these three transports.
The next page tries the fourth and fastest design, the core compiled to a native shared library and called through ctypes, and shows the real error this image gives for it.
Exercises
- Run the timing with two hundred calls per series and compare the medians.
- Start the API on another port and measure two clients at once.
- Measure the Python service of the earlier page and put it in the table.
- Explain which way you would choose for a command-line tool, a batch job and a web service.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which way had the lowest median per call?
Answer
The daemon, at 0.047 ms
The table quotes the three medians.
-
Why is a process per call so slow?
Answer
Every call starts the .NET runtime
The startup of the runtime is the whole cost.
-
What does this program print?
Console.WriteLine(85.309);Answer
85.309
The median of the process way is the measured 85.309.
-
How many series and calls per series?
Answer
Five series of twenty calls
The command is timing 5 20.
-
What does this program print?
Console.WriteLine(0.047 < 2.271);Answer
True
The daemon is faster than the API per call.
-
What does the API price buy?
Answer
A caller on another machine and a status code
The page says what HTTP buys for its 78 times.
-
Why was the first daemon series slower?
Answer
The first call pays for the started process
The first series was 1.403 against about 0.047 after.
-
What does this program print?
Console.WriteLine((int)Math.Round(2.271 / 0.047));Answer
48
2.271 divided by 0.047 is about 48.
-
What should a tool that runs once per invocation do?
Answer
Start a process per invocation
The page says the fastest number is not the choice.
-
Which way should a service that handles many requests use?
Answer
The core in process, not any of these transports
The page says a high-traffic service keeps the core in process.
The C# code on this page was built with the .NET SDK 10.0.303 and the Python code was run with Python 3.14.10, on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev