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:

WaySeries (ms per call)MinMedian
a new process per call84.539, 110.269, 84.313, 85.309, 97.04484.31385.309
the daemon on stdin and stdout1.403, 0.049, 0.047, 0.046, 0.0470.0460.047
the minimal API over HTTP5.962, 2.533, 2.101, 2.271, 2.0902.0902.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:

WaySeries (µs per call)MinMedian
the native library through ctypes3.42, 3.49, 3.50, 3.52, 4.783.423.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:

ItemValue
CPUIntel Core i3-6006U at 2.00 GHz, 4 processors, x86_64
SystemAlpine 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
Python3.14.10, python3 python/client.py timing 5 20
Callsfive series of twenty calls per way, per-call averages, min and median

How to read them

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

  1. Run the timing with two hundred calls per series and compare the medians.
  2. Start the API on another port and measure two clients at once.
  3. Measure the Python service of the earlier page and put it in the table.
  4. 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.

  1. Which way had the lowest median per call?

    Answer

    The daemon, at 0.047 ms

    The table quotes the three medians.

  2. Why is a process per call so slow?

    Answer

    Every call starts the .NET runtime

    The startup of the runtime is the whole cost.

  3. What does this program print? Console.WriteLine(85.309);

    Answer

    85.309

    The median of the process way is the measured 85.309.

  4. How many series and calls per series?

    Answer

    Five series of twenty calls

    The command is timing 5 20.

  5. What does this program print? Console.WriteLine(0.047 < 2.271);

    Answer

    True

    The daemon is faster than the API per call.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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