1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import org.junit.Test;
|
---|
7 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
8 | import org.openstreetmap.josm.data.coor.LatLonTest;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Checks that rounding of coordinates is not too slow.
|
---|
12 | */
|
---|
13 | public class RoundingPerformanceTest {
|
---|
14 |
|
---|
15 | private static double oldRoundToOsmPrecision(double value) {
|
---|
16 | // Old method, causes rounding errors, but efficient
|
---|
17 | return Math.round(value / LatLon.MAX_SERVER_PRECISION) * LatLon.MAX_SERVER_PRECISION;
|
---|
18 | }
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Checks that rounding of coordinates is not too slow.
|
---|
22 | */
|
---|
23 | @Test
|
---|
24 | public void test() {
|
---|
25 | final int n = 1000000;
|
---|
26 | long start = System.nanoTime();
|
---|
27 | for (int i = 0; i < n; i++) {
|
---|
28 | for (double value : LatLonTest.SAMPLE_VALUES) {
|
---|
29 | oldRoundToOsmPrecision(value);
|
---|
30 | }
|
---|
31 | }
|
---|
32 | long end = System.nanoTime();
|
---|
33 | long oldTime = end-start;
|
---|
34 | System.out.println("Old time: "+oldTime/1000000.0 + " ms");
|
---|
35 |
|
---|
36 | start = System.nanoTime();
|
---|
37 | for (int i = 0; i < n; i++) {
|
---|
38 | for (double value : LatLonTest.SAMPLE_VALUES) {
|
---|
39 | LatLon.roundToOsmPrecision(value);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | end = System.nanoTime();
|
---|
43 | long newTime = end-start;
|
---|
44 | System.out.println("New time: "+newTime/1000000.0 + " ms");
|
---|
45 |
|
---|
46 | assertTrue(newTime <= oldTime*12);
|
---|
47 | }
|
---|
48 | }
|
---|