Skip to content

Remove recursion, speed up modulus #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ node_modules
mason_packages
.toolchain
.mason
local.env
local.env
xcode-project
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/mason.cmake)

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

# configure optimization
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down Expand Up @@ -50,6 +53,18 @@ add_executable(unit-tests ${TEST_SOURCES})
find_package(Threads REQUIRED)
file(GLOB BENCH_SOURCES bench/*.cpp)
add_executable(bench-tests ${BENCH_SOURCES})
if(BENCHMARK_BIG_O)
message("-- BENCHMARK_BIG_O=1")
target_compile_definitions(bench-tests PUBLIC BENCHMARK_BIG_O=1)
endif()
if(BENCHMARK_100M)
message("-- BENCHMARK_100M=1")
target_compile_definitions(bench-tests PUBLIC BENCHMARK_100M=1)
endif()
if(BENCHMARK_10M)
message("-- BENCHMARK_10M=1")
target_compile_definitions(bench-tests PUBLIC BENCHMARK_10M=1)
endif()

#examples
add_executable(triangulate-geojson examples/triangulate_geojson.cpp)
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Whether to turn compiler warnings into errors
export WERROR ?= true
export BUILD_DIR ?= cmake-build
export XCODE_PROJ_DIR ?= xcode-project

default: release

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

xcode:
mkdir -p ./$(XCODE_PROJ_DIR) && cd ./$(XCODE_PROJ_DIR) && cmake -G Xcode ../

test:
@if [ -f ./$(BUILD_DIR)/unit-tests ]; then ./$(BUILD_DIR)/unit-tests; else echo "Please run 'make release' or 'make debug' first" && exit 1; fi

Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ int main() {

```
Run on (4 X 2300 MHz CPU s)
2018-09-26 09:28:34
2018-09-29 09:27:28
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_45K_geojson_nodes 24 ms 24 ms 29
BM_uniform/2000 1 ms 1 ms 887
BM_uniform/100000 66 ms 66 ms 9
BM_uniform/200000 158 ms 155 ms 4
BM_uniform/500000 441 ms 439 ms 2
BM_uniform/1000000 1062 ms 1058 ms 1
BM_45K_geojson_nodes 22 ms 22 ms 32
BM_uniform/2000 1 ms 1 ms 982
BM_uniform/100000 63 ms 62 ms 9
BM_uniform/200000 140 ms 140 ms 4
BM_uniform/500000 400 ms 399 ms 2
BM_uniform/1000000 994 ms 993 ms 1
```

Library is ~10% faster then JS version for 1M uniform points ([details](https://github.com/delfrrr/delaunator-cpp/pull/8#issuecomment-422690056))
12 changes: 12 additions & 0 deletions bench/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

std::vector<double> generate_uniform(std::size_t n) {
std::vector<double> coords;
coords.reserve(2 * n);
std::srand(350);
double norm = static_cast<double>(RAND_MAX) / 1e3;
for (size_t i = 0; i < n; i++) {
Expand All @@ -31,9 +32,20 @@ void BM_uniform(benchmark::State& state) {
while (state.KeepRunning()) {
delaunator::Delaunator delaunator(coords);
}
state.SetComplexityN(state.range(0));
}

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

#if BENCHMARK_BIG_O
BENCHMARK(BM_uniform)->RangeMultiplier(2)->Range(1 << 12, 1 << 22)->Unit(benchmark::kMillisecond)->Complexity();
#endif
#if BENCHMARK_10M
BENCHMARK(BM_uniform)->Arg(1000000 * 10)->Unit(benchmark::kMillisecond);
#endif
#if BENCHMARK_100M
BENCHMARK(BM_uniform)->Arg(1000000 * 100)->Unit(benchmark::kMillisecond);
#endif

BENCHMARK_MAIN()
2 changes: 1 addition & 1 deletion examples/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ inline std::string read_file(const char* filename) {
}
}

inline std::vector<double> get_geo_json_points(std::string const& json) {
inline std::vector< double> get_geo_json_points(std::string const& json) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to make this change?

rapidjson::Document document;
if(document.Parse(json.c_str()).HasParseError()) {
throw std::runtime_error("Cannot parse JSON");
Expand Down
Loading