Created
February 8, 2019 15:18
-
-
Save crhallberg/da8716bd44031bf8812f48bf49d3b2e6 to your computer and use it in GitHub Desktop.
QuadTree Save/Load Benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// package.json dependency: | |
// "ct-quadtree": "git://github.com/crhallberg/QuadTree.git#save-load" | |
const { QuadTree, Rectangle, Point } = require("ct-quadtree"); | |
function benchmark(msg, func) { | |
const NS_PER_SEC = 1e9; | |
console.log(msg); | |
const start = process.hrtime(); | |
let ret = func(); | |
const diff = process.hrtime(start); | |
console.log(`\t${(diff[0] * NS_PER_SEC + diff[1]) / 1000000}ms`); | |
return ret; | |
} | |
// http://download.geonames.org/export/zip/US.zip | |
var lineReader = require("readline").createInterface({ | |
input: require("fs").createReadStream("US.txt"), | |
}); | |
let points = []; | |
lineReader.on("line", function(line) { | |
parts = line.split("\t"); | |
const newPoint = new Point(parts[9], parts[10], { | |
zipcode: parts[1], | |
city: parts[2], | |
state: parts[3], | |
}); | |
points.push(newPoint); | |
}); | |
// When we reach the end of the file | |
lineReader.on("close", function() { | |
const testQuad = benchmark("Building from points....", () => { | |
const quad = new QuadTree(new Rectangle(0, 0, 180, 360), 4); | |
points.forEach(point => quad.insert(point)); | |
// Test query and return tree | |
quad.closest(new Point(39.830129, -75.425761), 1, 0.01); | |
return quad; | |
}); | |
// Generate JSON | |
const obj = JSON.parse(testQuad.toJSON()); | |
benchmark("Loading from JSON....", () => { | |
const quad = QuadTree.fromJSON(obj); | |
// Test query and return tree | |
quad.closest(new Point(39.830129, -75.425761), 1, 0.01); | |
return quad; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment