Skip to content

Commit c1521f6

Browse files
authored
Merge pull request delfrrr#12 from delfrrr/remove-recursion
Remove recursion, speed up modulus
2 parents 393cfc3 + 6e97993 commit c1521f6

File tree

7 files changed

+171
-115
lines changed

7 files changed

+171
-115
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ node_modules
1414
mason_packages
1515
.toolchain
1616
.mason
17-
local.env
17+
local.env
18+
xcode-project

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
77
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/mason.cmake)
88

99
option(WERROR "Add -Werror flag to build (turns warnings into errors)" ON)
10+
option(BENCHMARK_BIG_O "Calculate Big O in benchmark" OFF)
11+
option(BENCHMARK_100M "Run against 100M points" OFF)
12+
option(BENCHMARK_10M "Run against 100M points" OFF)
1013

1114
# configure optimization
1215
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
@@ -50,6 +53,18 @@ add_executable(unit-tests ${TEST_SOURCES})
5053
find_package(Threads REQUIRED)
5154
file(GLOB BENCH_SOURCES bench/*.cpp)
5255
add_executable(bench-tests ${BENCH_SOURCES})
56+
if(BENCHMARK_BIG_O)
57+
message("-- BENCHMARK_BIG_O=1")
58+
target_compile_definitions(bench-tests PUBLIC BENCHMARK_BIG_O=1)
59+
endif()
60+
if(BENCHMARK_100M)
61+
message("-- BENCHMARK_100M=1")
62+
target_compile_definitions(bench-tests PUBLIC BENCHMARK_100M=1)
63+
endif()
64+
if(BENCHMARK_10M)
65+
message("-- BENCHMARK_10M=1")
66+
target_compile_definitions(bench-tests PUBLIC BENCHMARK_10M=1)
67+
endif()
5368

5469
#examples
5570
add_executable(triangulate-geojson examples/triangulate_geojson.cpp)

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Whether to turn compiler warnings into errors
33
export WERROR ?= true
44
export BUILD_DIR ?= cmake-build
5+
export XCODE_PROJ_DIR ?= xcode-project
56

67
default: release
78

@@ -11,6 +12,9 @@ release:
1112
debug:
1213
mkdir -p ./$(BUILD_DIR) && cd ./$(BUILD_DIR) && cmake ../ -DCMAKE_BUILD_TYPE=Debug -DWERROR=$(WERROR) && VERBOSE=1 cmake --build .
1314

15+
xcode:
16+
mkdir -p ./$(XCODE_PROJ_DIR) && cd ./$(XCODE_PROJ_DIR) && cmake -G Xcode ../
17+
1418
test:
1519
@if [ -f ./$(BUILD_DIR)/unit-tests ]; then ./$(BUILD_DIR)/unit-tests; else echo "Please run 'make release' or 'make debug' first" && exit 1; fi
1620

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ int main() {
4848

4949
```
5050
Run on (4 X 2300 MHz CPU s)
51-
2018-09-26 09:28:34
51+
2018-09-29 09:27:28
5252
------------------------------------------------------------
5353
Benchmark Time CPU Iterations
5454
------------------------------------------------------------
55-
BM_45K_geojson_nodes 24 ms 24 ms 29
56-
BM_uniform/2000 1 ms 1 ms 887
57-
BM_uniform/100000 66 ms 66 ms 9
58-
BM_uniform/200000 158 ms 155 ms 4
59-
BM_uniform/500000 441 ms 439 ms 2
60-
BM_uniform/1000000 1062 ms 1058 ms 1
55+
BM_45K_geojson_nodes 22 ms 22 ms 32
56+
BM_uniform/2000 1 ms 1 ms 982
57+
BM_uniform/100000 63 ms 62 ms 9
58+
BM_uniform/200000 140 ms 140 ms 4
59+
BM_uniform/500000 400 ms 399 ms 2
60+
BM_uniform/1000000 994 ms 993 ms 1
6161
```
6262

6363
Library is ~10% faster then JS version for 1M uniform points ([details](https://github.com/delfrrr/delaunator-cpp/pull/8#issuecomment-422690056))

bench/run.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
std::vector<double> generate_uniform(std::size_t n) {
99
std::vector<double> coords;
10+
coords.reserve(2 * n);
1011
std::srand(350);
1112
double norm = static_cast<double>(RAND_MAX) / 1e3;
1213
for (size_t i = 0; i < n; i++) {
@@ -31,9 +32,20 @@ void BM_uniform(benchmark::State& state) {
3132
while (state.KeepRunning()) {
3233
delaunator::Delaunator delaunator(coords);
3334
}
35+
state.SetComplexityN(state.range(0));
3436
}
3537

3638
BENCHMARK(BM_45K_geojson_nodes)->Unit(benchmark::kMillisecond);
3739
BENCHMARK(BM_uniform)->Arg(2000)->Arg(100000)->Arg(200000)->Arg(500000)->Arg(1000000)->Unit(benchmark::kMillisecond);
3840

41+
#if BENCHMARK_BIG_O
42+
BENCHMARK(BM_uniform)->RangeMultiplier(2)->Range(1 << 12, 1 << 22)->Unit(benchmark::kMillisecond)->Complexity();
43+
#endif
44+
#if BENCHMARK_10M
45+
BENCHMARK(BM_uniform)->Arg(1000000 * 10)->Unit(benchmark::kMillisecond);
46+
#endif
47+
#if BENCHMARK_100M
48+
BENCHMARK(BM_uniform)->Arg(1000000 * 100)->Unit(benchmark::kMillisecond);
49+
#endif
50+
3951
BENCHMARK_MAIN()

examples/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ inline std::string read_file(const char* filename) {
2222
}
2323
}
2424

25-
inline std::vector<double> get_geo_json_points(std::string const& json) {
25+
inline std::vector< double> get_geo_json_points(std::string const& json) {
2626
rapidjson::Document document;
2727
if(document.Parse(json.c_str()).HasParseError()) {
2828
throw std::runtime_error("Cannot parse JSON");

0 commit comments

Comments
 (0)