Performance
How fast mathconv converts, what the published throughput figures measure, and how to get that speed from Python with batches and several cores.
You are reading the documentation of the development version on the dev branch, which is not released. The latest release is 0.1.0a4: this page in 0.1.0a4.
Published figures#
The README states the throughput of the current release: the
number of formulas converted per second on one core of a GitHub-hosted
Linux runner, from MathML to LaTeX, through Converter.convert_batch
and through the Rust core. The figures are for typical formulas,
real formulas of up to 60 elements, which make up almost all formulas
in real documents. Large tables and formulas of thousands of elements
take longer.
Each GitHub Release carries the full measurement as throughput.json:
for each file of the benchmark set and each pair of formats, the rate
through the Rust core, through mathconv.convert with one formula per
call, and through convert_batch. Its release notes end with a
paragraph of the figures. The figures are wall-clock times on shared virtual
machines, so they vary from run to run by a few percent and from
machine to machine by more; they describe the speed you can expect on
a similar machine. Throughput
describes how they are measured.
Converting in batches#
Each Python call has a fixed cost: crossing from Python into the
extension, and building the Python strings and objects of the result.
In the published measurement, convert_batch is faster than one call
per formula, most of all for short formulas. To convert many formulas:
- Create one
Converterand reuse it.mathconv.convertchecks its formats and options on every call. - Pass lists of formulas to
convert_batchorresult_batchrather than callingconvertin a loop. A batch call converts all its formulas in one call into the extension, reads eachstrin place, and releases the GIL while it converts. - Use
convert_batchwhen you need only the outputs.result_batchalso builds aResultand its fallbacks for each formula.
A batch call returns all its outputs at once, so for a large or streamed input, convert it in chunks of a few thousand formulas, which keeps memory bounded at little cost in speed.
Using several cores#
A batch call converts on one core. Because it releases the GIL, several threads that call the batch methods run in parallel, and one converter can be shared between them:
import mathconv
from concurrent.futures import ThreadPoolExecutor
converter = mathconv.Converter()
formulas = [f"<math><msub><mi>x</mi><mn>{i}</mn></msub></math>" for i in range(10_000)]
chunks = [formulas[start : start + 1_000] for start in range(0, len(formulas), 1_000)]
with ThreadPoolExecutor(max_workers=4) as pool:
outputs = [output for chunk in pool.map(converter.convert_batch, chunks) for output in chunk]
outputs[:3]
# ['x_0', 'x_1', 'x_2']
Worker processes, as with multiprocessing or
concurrent.futures.ProcessPoolExecutor, work as well. A converter
cannot be pickled, so each process creates its own, for example in the
pool's initializer, and receives only the formulas.
Measuring#
To measure on your own machine and data, time convert_batch on a
list of your formulas with time.perf_counter, after a first call,
which grows the buffers that later calls on the same thread reuse.
Contributors can run cargo xtask throughput in a checkout of the
repository, which measures the benchmark set as the published figures
do.