Performance

The cost of meshsearch’s construction, memory and queries, measured from Python against scipy’s cKDTree and scikit-learn’s BallTree, and the effect of the cell side and of the number of workers. The advice drawn from these measurements is in the guide. Every measured number, at every size, is in the data files.

Summary

Ten million points, a cell side of one mean separation s, batched queries of 10,000 fresh points in random order, on one thread. Times of queries are in microseconds (µs) per query; periodic is the uniform set with all three axes periodic, against cKDTree with boxsize=1.

uniform: meshsearch

cKDTree

clustered: meshsearch

cKDTree

periodic: meshsearch

cKDTree

construction (s)

0.84

6.07

0.74

5.86

0.72

6.10

memory (MiB)

819

398

780

334

547

427

nearest (µs per query)

1.26

2.51

1.55

2.77

1.85

3.03

k = 10 (µs per query)

3.31

5.19

3.34

5.24

4.36

7.07

radius 2 s (µs per query)

6.05

9.79

7.91

45.99

8.37

14.29

Construction is 7.2 to 8.5 times faster than cKDTree’s, the nearest object 1.6 to 2.0 times and the ten nearest 1.6 times; the radius query is 1.6 to 1.7 times faster on uniform and periodic data and 5.8 times on clustered data. The cost is memory: the process grows by 1.3 to 2.3 times as much as with cKDTree, and on clustered data the grid allocates 820 MiB, more than that growth shows (memory).

The summary is at 1 s, where the nearest object is fastest at this size; at 2 s, where the guide suggests starting, the process grows by about as much as with cKDTree (12 per cent less on the periodic set), the radius query is 3.6 to 10.5 times faster and the nearest and the ten nearest 1.3 to 2.0 times.

Method

machine

Apple M1 Pro, 10 cores (8 performance, 2 efficiency), 16 GiB, macOS 14.5

compiler

Apple clang 15, CMake Release build (-O3)

Python

3.14.7, numpy 2.5.5, scipy 1.18.1, scikit-learn 1.9.1

Each row is one library at one size, run in its own process, on one thread except in the workers table, with no other benchmark running. A query time is the median of three runs; the cell-side sweep was run five times, and gives the median of its five such medians. Each run draws 10,000 new query points in random order, the same draws for every library, so that no run finds the cells or tree nodes it visits already in the processor’s cache. Batched is one call for all 10,000 points; per call is one Python call per point. Memory is the growth of the process’s resident size during construction. Every structure is built with its default parameters, meshsearch with the unit cube as explicit limits. The trees keep a reference to the caller’s array; meshsearch copies the coordinates, which its growth includes (229 MiB at 10⁷ points). cKDTree also returns distances, meshsearch indices only.

Variation between runs. Across the five runs of the sweep, the spread of a row (largest minus smallest, over the median) was typically 5 to 7 per cent for times and at most 22 per cent, and for memory under 1 per cent in most rows and up to 21 per cent in the smallest clustered ones; a single run varies more than these medians, and smaller differences between rows are not significant.

Data

The mean separation s of N points in a volume V is the cube root of V/N. Cell sides and radii are given in units of s; the radius query uses 2 s and the k-nearest query k = 10.

  • Uniform: points and query points uniform in the unit cube. A ball of radius 2 s holds about 33 objects.

  • Clustered: a Thomas process in the unit cube. Nine tenths of the points lie in clusters of about 1,000, each a Gaussian of width s around a uniformly drawn centre; the rest are uniform. Query points are fresh draws from the same clusters, so a ball of radius 2 s holds about 410 objects.

Against cKDTree and BallTree

Batched queries. The trees have no cell side; meshsearch is shown at 1, 2 and 4 s. Sizes of 10⁵ points are in the data files.

Uniform

points

meshsearch, 1 s

meshsearch, 2 s

meshsearch, 4 s

cKDTree

BallTree

10⁶

construction (s)

0.061

0.029

0.020

0.382

0.367

memory (MiB)

83

42

37

52

97

nearest (µs per query)

0.99

0.88

1.34

1.64

15.73

k = 10 (µs per query)

2.88

2.10

3.46

3.58

26.60

radius (µs per query)

5.04

1.58

1.32

6.81

8.46

10⁷

construction (s)

0.845

0.483

0.328

6.069

6.583

memory (MiB)

819

412

361

398

176

nearest (µs per query)

1.26

1.46

2.10

2.51

55.31

k = 10 (µs per query)

3.31

3.19

4.69

5.19

78.36

radius (µs per query)

6.05

2.74

2.20

9.79

15.21

Clustered

points

meshsearch, 1 s

meshsearch, 2 s

meshsearch, 4 s

cKDTree

BallTree

10⁶

construction (s)

0.051

0.024

0.020

0.353

0.387

memory (MiB)

67

41

36

48

90

nearest (µs per query)

0.97

1.20

1.75

1.69

48.84

k = 10 (µs per query)

2.51

2.90

3.94

3.41

61.01

radius (µs per query)

5.92

3.22

2.66

34.49

22.36

10⁷

construction (s)

0.742

0.398

0.237

5.859

6.505

memory (MiB)

780

355

323

334

174

nearest (µs per query)

1.55

1.98

2.52

2.77

130.12

k = 10 (µs per query)

3.34

3.97

5.03

5.24

174.98

radius (µs per query)

7.91

4.37

3.55

45.99

39.12

Construction is 6.0 to 27 times faster than either tree’s. At 1 and 2 s meshsearch is faster than cKDTree on every query, by 1.2 to 11 times, most for the radius query; at 4 s its radius query is 4.5 to 13 times faster, while the nearest and the ten nearest are within 23 per cent of cKDTree either way. At 10⁶ and 10⁷ points BallTree is the slowest of the three on every query except the clustered radius query, where it is faster than cKDTree.

Periodic boxes

All three axes periodic, against cKDTree with boxsize=1; BallTree has no periodic mode. Batched queries. The cell side must divide the period, so at 10⁷ points the grid used 0.004630 for a requested 0.004642 (1 s), 0.009259 for 0.009283 (2 s) and 0.018519 for 0.018566 (4 s).

data

points

meshsearch, 1 s

meshsearch, 2 s

meshsearch, 4 s

cKDTree, boxsize

uniform

10⁶

construction (s)

0.045

0.024

0.020

0.382

memory (MiB)

55

38

36

52

nearest (µs per query)

1.17

1.14

1.61

2.15

k = 10 (µs per query)

3.10

2.25

3.78

4.71

radius (µs per query)

5.85

1.77

1.42

10.62

10⁷

construction (s)

0.719

0.473

0.318

6.096

memory (MiB)

547

378

356

427

nearest (µs per query)

1.85

1.95

2.37

3.03

k = 10 (µs per query)

4.36

3.58

4.78

7.07

radius (µs per query)

8.37

3.08

2.36

14.29

clustered

10⁶

construction (s)

0.035

0.021

0.019

0.363

memory (MiB)

40

38

36

50

nearest (µs per query)

1.25

1.53

2.12

2.38

k = 10 (µs per query)

2.95

3.15

4.26

5.26

radius (µs per query)

6.93

3.45

2.88

43.95

10⁷

construction (s)

0.601

0.379

0.258

6.005

memory (MiB)

509

320

318

363

nearest (µs per query)

1.93

2.31

3.02

3.04

k = 10 (µs per query)

4.02

4.47

5.89

6.77

radius (µs per query)

9.38

4.94

4.31

54.27

Against the same grid without periodic axes, batched queries took 2 to 47 per cent more time, and at 1 s meshsearch remains faster than cKDTree on every query, by 1.5 to 6.3 times. The periodic grid needs less memory, since its table of cell offsets is smaller: 547 against 819 MiB at 10⁷ points and 1 s.

Per call

One Python call per query point, in µs per query, with the same fresh query points as the batched tables.

data

points

query (µs per query)

meshsearch, 1 s

meshsearch, 2 s

meshsearch, 4 s

cKDTree

BallTree

uniform

10⁶

nearest

1.89

1.66

2.12

9.50

47.53

k = 10

4.10

3.67

4.81

13.05

57.12

radius

6.81

3.33

2.78

8.66

850.44

10⁷

nearest

1.98

2.21

2.96

10.08

87.20

k = 10

4.57

4.47

6.06

13.87

111.55

radius

7.38

4.23

3.65

11.58

8,510.85

clustered

10⁶

nearest

1.70

1.92

2.49

9.35

75.07

k = 10

3.91

4.33

5.18

12.63

95.97

radius

7.61

4.93

4.25

29.57

910.41

10⁷

nearest

2.25

2.68

3.07

10.57

162.01

k = 10

4.60

5.22

5.99

13.82

205.45

radius

9.78

6.32

5.24

33.60

8,201.07

A meshsearch call for the nearest object costs 0.55 to 0.9 µs more than the same query in a batch, including unpacking a numpy row, and one for the radius query up to 2.0 µs more. meshsearch is faster per call on every query in every row, by at least 1.27 times; the periodic rows are in the data files.

The cost of the call itself, measured separately on 10⁶ uniform points at 1 s by repeating 100 query points (median of five runs of 20,000 calls, benchmark.py --call-overhead):

query

µs per call

nearest, three Python floats

0.25

nearest, a numpy float64 row unpacked

0.79

k = 10

1.51

radius 2 s

2.95

count 2 s

1.21

contains

0.05

is_alive

0.04

With scipy and scikit-learn imported into the same process, the figures change by up to 15 per cent.

Large batches

A large batch is answered internally in the order of the cells, and the results are returned in input order. One call of 10⁵ or 10⁶ fresh uniform query points in random order, on grids with no periodic axis; the input order columns are the same points in batches small enough to be answered in the order given. In µs per query.

grid

queries

query (µs per query)

meshsearch, 1 s

input order, 1 s

meshsearch, 4 s

input order, 4 s

cKDTree

10⁶

10⁵

nearest

0.51

0.93

0.92

1.33

1.59

10⁶

10⁵

k = 10

1.38

2.58

2.88

3.34

3.55

10⁶

10⁵

radius

2.75

4.68

0.81

1.24

6.66

10⁶

10⁶

nearest

0.57

0.93

0.92

1.28

1.57

10⁶

10⁶

k = 10

1.46

2.56

2.90

3.41

3.55

10⁶

10⁶

radius

2.64

4.76

0.90

1.23

6.82

10⁷

10⁵

nearest

0.80

1.21

1.11

2.07

2.54

10⁷

10⁵

k = 10

2.21

3.19

3.11

4.75

5.27

10⁷

10⁵

radius

3.45

6.02

1.03

2.18

9.60

10⁷

10⁶

nearest

0.60

1.20

1.04

2.08

2.51

10⁷

10⁶

k = 10

1.51

3.26

3.03

4.58

5.32

10⁷

10⁶

radius

2.89

6.00

0.94

2.17

10.04

The cell order makes queries 1.4 to 2.2 times faster at 1 s and 1.2 to 2.3 times faster at 4 s. On large batches at 1 s meshsearch is 2.4 to 4.2 times faster than cKDTree on every query.

Workers

Batched queries at 1 s with several workers, 10,000 fresh query points per run, against cKDTree with workers=1 and workers=-1; count is count_objects against query_ball_point with return_length=True, within 2 s. On this machine −1 means ten threads, on eight performance and two efficiency cores. In µs per query.

data

points

query (µs per query)

meshsearch, workers=1

workers=2

workers=4

workers=8

workers=-1

cKDTree, workers=1

cKDTree, workers=-1

uniform

10⁶

nearest

0.926

0.488

0.249

0.150

0.136

1.659

0.354

k = 10

2.525

1.296

0.659

0.374

0.325

3.793

0.703

radius

4.623

2.469

1.276

0.744

0.639

7.401

1.570

count

3.345

1.730

0.848

0.483

0.415

5.668

0.944

10⁷

nearest

1.233

0.646

0.367

0.198

0.183

2.491

0.530

k = 10

3.246

1.650

0.862

0.471

0.404

5.284

0.943

radius

6.083

3.234

1.685

0.947

0.822

9.766

2.131

count

4.341

2.232

1.205

0.627

0.537

7.870

1.451

clustered

10⁶

nearest

1.015

0.546

0.329

0.188

0.181

1.624

0.657

k = 10

2.521

1.299

0.751

0.428

0.342

3.516

1.372

radius

5.961

3.158

1.901

1.179

1.053

33.927

12.859

count

3.750

1.981

1.234

0.647

0.516

11.355

2.046

10⁷

nearest

1.548

0.848

0.456

0.268

0.320

2.313

0.506

k = 10

3.337

1.775

0.930

0.525

0.599

5.930

1.012

radius

7.787

4.409

2.441

1.536

1.761

40.862

13.929

count

5.758

3.089

1.643

0.942

1.147

18.643

2.908

Two workers make a batch 1.8 to 2.0 times faster than one, four 3.0 to 3.9 times and eight 5.1 to 6.9 times; ten, with -1, take 4 to 20 per cent less time than eight, except on clustered data at 10⁷ points, where they take 14 to 22 per cent more. With workers=-1 on both sides meshsearch is 1.6 to 12 times faster than cKDTree, most for the radius query, and on one thread its count is 1.7 to 3.2 times faster. These batches last 1.4 to 18 ms, so they vary more between runs than the single-thread rows.

Cell side

Batched query times at ten million points and six cell sides, with the occupancy of the cells: the fraction of empty cells, the mean count in the non-empty ones and the 99th percentile of the count over all cells. The radius stays 2 s. Query points are drawn as for the other tables, and each time is the median of the five sweeps (method). The sweep at 10⁶ points, which also covers 0.5 s, is in the data files.

Uniform

cell side

cells

empty

mean in non-empty (objects)

99th percentile (objects)

construction (s)

memory (MiB)

nearest (µs per query)

k = 10 (µs per query)

radius (µs per query)

1 s

10,077,696

37 %

1.6

4

0.79

819

1.24

3.27

5.90

1.5 s

2,985,984

3.5 %

3.5

8

0.58

492

1.33

2.89

3.57

2 s

1,259,712

0 %

7.9

15

0.47

412

1.53

3.20

2.81

2.5 s

658,503

0 %

15.2

25

0.42

384

1.67

3.48

2.41

3 s

373,248

0 %

26.8

40

0.38

371

1.71

4.14

2.33

4 s

157,464

0 %

63.5

83

0.31

360

2.16

4.63

2.20

Clustered

cell side

cells

empty

mean in non-empty (objects)

99th percentile (objects)

construction (s)

memory (MiB)

nearest (µs per query)

k = 10 (µs per query)

radius (µs per query)

1 s

10,077,696

80 %

5.0

24

0.73

779

1.56

3.32

7.89

1.5 s

2,985,984

59 %

8.2

76

0.49

446

1.79

3.73

5.74

2 s

1,259,712

35 %

12.2

165

0.40

354

2.01

3.99

4.70

2.5 s

658,503

16 %

18.1

284

0.34

345

2.05

4.12

4.08

3 s

373,248

4.4 %

28.0

434

0.28

332

2.23

4.34

4.07

4 s

157,464

0.1 %

63.6

730

0.24

322

2.55

5.01

3.58

On uniform data the nearest object is fastest at 1 s, the ten nearest at 1.5 s and the radius query at 4 s, 6 per cent ahead of 3 s and 2.7 times faster than at 1 s; at 10⁶ points the optima are the same within one step of the sweep.

On clustered data the nearest and the ten nearest are fastest at 1 s, where four fifths of the cells are empty, and slow down as the cells grow: at 4 s one cell in a hundred holds 730 objects or more, eleven times the mean. The radius query is fastest at 4 s, the largest side of the sweep, 14 per cent ahead of 2.5 and 3 s at 10⁷ points and 3 per cent ahead of 3 s at 10⁶.

At 10⁶ points a cell side of 0.5 s takes 6.0 (uniform) and 6.4 (clustered) times the memory of 1 s and slows every query except the nearest object on uniform data, which it finds in 0.76 against 0.91 µs.

A cell side equal to the radius costs no more than one just above or below it: on 10⁶ uniform points, between 1.98 and 2.02 s and between 2.98 and 3.02 s, the radius query changed no more than it varies between runs (radius_step.csv).

Memory

What a grid allocates, and its cost per object and per cell, is described in the guide. Measured from a C++ program, with no Python in the process (benchmarks/memory_cpp.cpp, three runs each), at 10⁷ points:

cell side

periodic axes

cells

allocated (MiB)

resident growth (MiB)

construction (s)

1 s

none

10,077,696

820.2

818–819

0.73–0.81

2 s

none

1,259,712

413.0

412

0.40–0.53

4 s

none

157,464

361.0

361

0.25–0.32

1 s

all three

10,077,696

548.0

547

0.57–0.64

2 s

all three

1,259,712

378.0

378

0.38–0.49

4 s

all three

157,464

356.1

356

0.25–0.33

The allocation is the same in every run and for both data sets, and nothing temporary is allocated: the peak growth equals the final growth. The resident size grew by the allocation within 3 MiB in every run at 10⁶ and 10⁷ points, except the 4 GiB grid of 10⁷ points at 0.5 s, which grew by 1.5 to 3.8 GiB. From Python the growth equals the allocation on uniform data, and on clustered data is 38 to 59 MiB (5 to 14 per cent) below it at 10⁷ points and up to 25 MiB below at 10⁶; plan with the allocation.

Not measured

Additions and removals; boxes elongated along one axis; clustering of other strengths or shapes; radii other than 2 s; the batched contains against the trees; workers at cell sides other than 1 s; GCC (the figures above are Apple clang) and Linux; machines other than the one above.

Data files

Every number measured for this page, at every size, in benchmarks/results/. Times are in µs per query, construction in seconds, memory in MiB.

  • comparison.csv: meshsearch at 1, 2 and 4 s, cKDTree and BallTree, uniform and clustered, 10⁵, 10⁶ and 10⁷ points, per call and batched.

  • periodic.csv: the periodic boxes, per call and batched.

  • large_batches.csv: the large batches, in cell order and in input order.

  • workers.csv: the batched queries with several workers.

  • cellside.csv and cellside_runs.csv: the cell-side sweep at 10⁶ points (0.5 to 4 s) and 10⁷ points (1 to 4 s), medians and each of the five runs, batched and per call.

  • radius_step.csv: the radius query around a cell side equal to the radius.

  • call_overhead.csv: the cost of one Python call.

  • memory_cpp.csv: the construction measured from C++, at 10⁶ and 10⁷ points, 0.5 to 4 s, with no periodic axis and with all three, each run.

The scripts are in benchmarks/; benchmarks/README.md describes each and its options. From a source checkout:

pip install -C cmake.build-type=Release .
pip install scipy scikit-learn psutil
python benchmarks/benchmark.py
python benchmarks/benchmark.py --periodic --sizes 1000000 10000000
python benchmarks/benchmark.py --large-batch --sizes 1000000 10000000 --factors 1 4
python benchmarks/benchmark.py --workers --sizes 1000000 10000000 --factors 1
python benchmarks/benchmark.py --call-overhead
python benchmarks/benchmark_cellsize.py --sweep
python benchmarks/benchmark_cellsize.py --radius-step
python benchmarks/benchmark_cellsize.py --memory-cpp ./memory_cpp

Each prints its rows as JSON. Most of the time of the first goes into BallTree’s per-call radius queries at 10⁷ points. The sweep also builds the grid of 0.5 s at 10⁷ points, which needs about 4 GiB of memory and is not in the tables.