Skip to content

Commit

Permalink
added perf example for sweep
Browse files Browse the repository at this point in the history
There is a case when sweep line is still faster
  • Loading branch information
anvaka committed Oct 15, 2018
1 parent f657189 commit b570425
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ Bush shortens. Sweep line comes last.
Now Bush algorithm wins by huge margin. Bentley-Ottman comes second, and brute
force comes the last.

### Parallel Slanted lines

[![slanted](https://i.imgur.com/vYAZzNvm.png)](https://anvaka.github.io/isect/?isAsync=false&p0=1000&p1=40&generator=parallelSlanted&algorithm=sweep&stepsPerFrame=1)

* **Sweep: 1000 slanted, not intersect x 622 ops/sec ±1.23% (91 runs sampled)**
* Brute: 1000 slanted, not intersect x 230 ops/sec ±2.37% (87 runs sampled)
* Bush: 1000 slanted, not intersect x 243 ops/sec ±1.07% (87 runs sampled)

In this example there too many lines, and none of them intersect. Furthermore, most of the
rectangular bounding boxes do intersect, which gives more work for the `bush` algorithm

# usage

Install the module from npm:
Expand Down
12 changes: 12 additions & 0 deletions demo/interactive/src/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export function random(count = 4, range = 100, seed) {
return lines;
}

export function parallelSlanted(count) {
var lines = [];
for (var i = 0; i < count; ++i) {
var x = -i, y = i;
lines.push({
from: {x, y},
to: {x: i, y: i + i}
})
}
return lines;
}

export function sparse(size = 50) {
var lines = [];
var rows = size, columns = size;
Expand Down
16 changes: 15 additions & 1 deletion perf/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
grid: grid,
complete: complete,
drunkGrid: drunkGrid,
sparse: sparse
sparse: sparse,
parallelSlanted: parallelSlanted
}

function sparse(size, seed) {
Expand All @@ -37,6 +38,19 @@ function sparse(size, seed) {
return lines;
}

function parallelSlanted(count) {
var lines = [];
for (var i = 0; i < count; ++i) {
var x = -i, y = i;
lines.push({
from: {x, y},
to: {x: i, y: i + i}
})
}
return lines;
}


function random(count = 4, range = 100, seed) {
if (seed !== undefined) {
prng = createRandom(seed);
Expand Down
17 changes: 16 additions & 1 deletion perf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var sparseLines = g.sparse(50, seed);
var bruteSparseLines = g.sparse(50, seed);
var bushSparseLines = g.sparse(50, seed);

var slanted = g.parallelSlanted(1000);
var bruteSlanted = g.parallelSlanted(1000);
var bushSlanted = g.parallelSlanted(1000);

var aLines = lines.map(x => [[x.from.x, x.from.y], [x.to.x, x.to.y]]);
var aRandomLines = randomLines.map(x => [[x.from.x, x.from.y], [x.to.x, x.to.y]]);

Expand Down Expand Up @@ -69,7 +73,18 @@ suite.add('Sweep: Circular lines 12x40', function() {
var res = bush(bushSparseLines).run();
if (res.length !== 358) throw new Error('Invalid number of intersections');
})

.add('Sweep: 1,000 slanted, not intersect', function() {
var res = sweep(slanted).run();
if (res.length !== 0) throw new Error('Invalid number of intersections');
})
.add('Brute: 1,000 slanted, not intersect', function() {
var res = brute(bruteSlanted).run();
if (res.length !== 0) throw new Error('Invalid number of intersections');
})
.add('Bush: 1,000 slanted, not intersect', function() {
var res = brute(bushSlanted).run();
if (res.length !== 0) throw new Error('Invalid number of intersections');
})
// .add('Alternative circular lines 12x40', function () {
// var res = alternativeImplementation(aRandomLines);
// if (res.length !== 1123) throw new Error('Invalid number of intersections');
Expand Down

0 comments on commit b570425

Please sign in to comment.