diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-03-27 16:59:27 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2013-11-15 17:38:45 +0100 |
commit | ece8b76a204847d664acb45bc5dc2e296fccc763 (patch) | |
tree | 83e05a2c252225b47a7c581c36e29a74c59f15ef /Algorithms/Part-I/5-KdTrees | |
parent | 60d3bf03904bc4f691d98b63ece8a1a7a482f182 (diff) | |
download | coursera-ece8b76a204847d664acb45bc5dc2e296fccc763.zip coursera-ece8b76a204847d664acb45bc5dc2e296fccc763.tar.gz |
Algorithms-I : 5-KdTrees: add Speed.javaalgs-I
Diffstat (limited to 'Algorithms/Part-I/5-KdTrees')
-rw-r--r-- | Algorithms/Part-I/5-KdTrees/Speed.java | 43 | ||||
-rwxr-xr-x | Algorithms/Part-I/5-KdTrees/run.sh | 3 |
2 files changed, 45 insertions, 1 deletions
diff --git a/Algorithms/Part-I/5-KdTrees/Speed.java b/Algorithms/Part-I/5-KdTrees/Speed.java new file mode 100644 index 0000000..ff8c535 --- /dev/null +++ b/Algorithms/Part-I/5-KdTrees/Speed.java @@ -0,0 +1,43 @@ + +public class Speed +{ + + public static void measure(Point2D p, KdTree k0) + { + Point2D np; + long t0; + long d0; + + t0 = System.nanoTime(); + np = k0.nearest(p); + d0 = System.nanoTime() - t0; + + StdOut.printf("Nearest Point: %s => %s %.3f [ms]\n", p, np, d0/1000.0); + } + + public static void main(String[] args) + { + String filename = args[0]; + In in = new In(filename); + + KdTree kdtree = new KdTree(); + while (!in.isEmpty()) { + double x = in.readDouble(); + double y = in.readDouble(); + Point2D p = new Point2D(x, y); + kdtree.insert(p); + } + + measure(new Point2D(0.5, 0.5), kdtree); + measure(new Point2D(0.5, 0.5), kdtree); + measure(new Point2D(0.3, 0.5), kdtree); + measure(new Point2D(0.3, 0.5), kdtree); + measure(new Point2D(0.5, 0.3), kdtree); + measure(new Point2D(0.5, 0.3), kdtree); + measure(new Point2D(0.2, 0.7), kdtree); + measure(new Point2D(0.2, 0.7), kdtree); + measure(new Point2D(0.1, 0.8), kdtree); + measure(new Point2D(0.1, 0.8), kdtree); + + } +} diff --git a/Algorithms/Part-I/5-KdTrees/run.sh b/Algorithms/Part-I/5-KdTrees/run.sh index 1ddfb96..5311ac6 100755 --- a/Algorithms/Part-I/5-KdTrees/run.sh +++ b/Algorithms/Part-I/5-KdTrees/run.sh @@ -13,11 +13,12 @@ for kls in $CLASSES; do done ~/algs4/bin/findbugs *.class -for kls in KdTreeVisualizer NearestNeighborVisualizer RangeSearchVisualizer; do +for kls in Speed KdTreeVisualizer NearestNeighborVisualizer RangeSearchVisualizer; do echo "javac ${kls}.java" && javac ${kls}.java done echo "RUN..." +java Speed input1M.txt java KdTreeVisualizer circle10.txt java RangeSearchVisualizer input10K.txt java NearestNeighborVisualizer input10K.txt |