File: bench_proj_trans.cpp

package info (click to toggle)
proj 9.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,800 kB
  • sloc: sh: 218,851; cpp: 171,440; sql: 83,614; python: 8,422; ansic: 6,254; yacc: 1,349; makefile: 33
file content (179 lines) | stat: -rw-r--r-- 6,682 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/******************************************************************************
 * Project:  PROJ
 * Purpose:  Benchmark
 * Author:   Even Rouault, <even.rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2022, Even Rouault, <even.rouault at spatialys.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

#include "proj.h"

#include <stdlib.h> // rand()

#include <chrono>
#include <cmath> // HUGE_VAL
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

static void usage() {
    printf("Usage: bench_proj_trans [(--source-crs|-s) string]\n");
    printf("                        [(--target-crs|-t) string]\n");
    printf("                        [(--pipeline|-p) string]\n");
    printf("                        [(--loops|-l) number]\n");
    printf("                        [--noise-x number] [--noise-y number]\n");
    printf("                        coord_comp_1 coord_comp_2 [coord_comp_3] "
           "[coord_comp_4]\n");
    printf("\n");
    printf("Both of --source-crs and --target_crs, or --pipeline must be "
           "specified.\n");
    printf("\n");
    printf("Example: bench_proj_trans -s EPSG:4326 -t EPSG:32631 49 2\n");
    exit(1);
}

int main(int argc, char *argv[]) {
    std::string sourceCRS;
    std::string targetCRS;
    std::string pipeline;
    int loops = 5 * 1000 * 1000;
    double coord_comp[4] = {0, 0, 0, HUGE_VAL};
    int coord_comp_counter = 0;
    double noiseX = 0;
    double noiseY = 0;
    for (int i = 1; i < argc; ++i) {
        if (strcmp(argv[i], "--source-crs") == 0 ||
            strcmp(argv[i], "-s") == 0) {
            if (i + 1 >= argc)
                usage();
            sourceCRS = argv[i + 1];
            ++i;
        } else if (strcmp(argv[i], "--target-crs") == 0 ||
                   strcmp(argv[i], "-t") == 0) {
            if (i + 1 >= argc)
                usage();
            targetCRS = argv[i + 1];
            ++i;
        } else if (strcmp(argv[i], "--pipeline") == 0 ||
                   strcmp(argv[i], "-p") == 0) {
            if (i + 1 >= argc)
                usage();
            pipeline = argv[i + 1];
            ++i;
        } else if (strcmp(argv[i], "--loops") == 0 ||
                   strcmp(argv[i], "-l") == 0) {
            if (i + 1 >= argc)
                usage();
            loops = atoi(argv[i + 1]);
            ++i;
        } else if (strcmp(argv[i], "--noise-x") == 0) {
            if (i + 1 >= argc)
                usage();
            noiseX = atof(argv[i + 1]);
            ++i;
        } else if (strcmp(argv[i], "--noise-y") == 0) {
            if (i + 1 >= argc)
                usage();
            noiseY = atof(argv[i + 1]);
            ++i;
        } else if (argv[i][0] == '-' &&
                   !(argv[i][1] >= '0' && argv[i][1] <= '9')) {
            usage();
        } else if (coord_comp_counter < 4) {
            coord_comp[coord_comp_counter++] = atof(argv[i]);
        } else {
            usage();
        }
    }
    if (coord_comp_counter < 2)
        usage();

    PJ_CONTEXT *ctxt = proj_context_create();
    PJ *P = nullptr;
    if (!pipeline.empty()) {
        P = proj_create(ctxt, pipeline.c_str());
    } else if (!sourceCRS.empty() && !targetCRS.empty()) {
        P = proj_create_crs_to_crs(ctxt, sourceCRS.c_str(), targetCRS.c_str(),
                                   nullptr);
    } else {
        usage();
    }
    if (P == nullptr) {
        exit(1);
    }
    PJ_COORD c;
    c.v[0] = coord_comp[0];
    c.v[1] = coord_comp[1];
    c.v[2] = coord_comp[2];
    c.v[3] = coord_comp[3];
    PJ_COORD c_ori = c;
    auto res = proj_trans(P, PJ_FWD, c);
    if (coord_comp_counter == 2) {
        printf("%.15g %.15g -> %.15g %.15g\n", c.v[0], c.v[1], res.v[0],
               res.v[1]);
    } else if (coord_comp_counter == 3) {
        printf("%.15g %.15g %.15g -> %.15g %.15g %.15g\n", c.v[0], c.v[1],
               c.v[2], res.v[0], res.v[1], res.v[2]);
    } else {
        printf("%.15g %.15g %.15g %.15g -> %.15g %.15g %.15g %.15g\n", c.v[0],
               c.v[1], c.v[2], c.v[3], res.v[0], res.v[1], res.v[2], res.v[3]);
    }

    // Start by timing just noise generation
    double dummy = 0;
    auto start_noise = std::chrono::system_clock::now();
    for (int i = 0; i < loops; ++i) {
        if (noiseX != 0)
            c.v[0] = c_ori.v[0] + noiseX * (2 * double(rand()) / RAND_MAX - 1);
        if (noiseY != 0)
            c.v[1] = c_ori.v[1] + noiseY * (2 * double(rand()) / RAND_MAX - 1);
        dummy += c.v[0];
        dummy += c.v[1];
    }
    auto end_noise = std::chrono::system_clock::now();

    auto start = std::chrono::system_clock::now();
    for (int i = 0; i < loops; ++i) {
        if (noiseX != 0)
            c.v[0] = c_ori.v[0] + noiseX * (2 * double(rand()) / RAND_MAX - 1);
        if (noiseY != 0)
            c.v[1] = c_ori.v[1] + noiseY * (2 * double(rand()) / RAND_MAX - 1);
        dummy += c.v[0];
        dummy += c.v[1];
        proj_trans(P, PJ_FWD, c);
    }
    auto end = std::chrono::system_clock::now();

    proj_destroy(P);
    proj_context_destroy(ctxt);

    auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
        (end - start) - (end_noise - start_noise));
    printf("Duration: %d ms\n", static_cast<int>(elapsed_ms.count()));
    printf("Throughput: %.02f million coordinates/s\n",
           1e-3 * static_cast<double>(loops) /
                   static_cast<double>(elapsed_ms.count()) +
               dummy * 1e-300);

    return 0;
}