From 1fa78d10b8fe711a4c0d35961a68048fa3ebd155 Mon Sep 17 00:00:00 2001 From: XInvisib1eX Date: Thu, 29 Apr 2021 18:51:51 +0300 Subject: [PATCH 01/18] improved julia code --- README.md | 8 ++++---- julia/stl.jl | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b9e97d6..fe26787 100644 --- a/README.md +++ b/README.md @@ -39,12 +39,12 @@ From the `julia` directory: ```console $ julia -O3 julia> ] -(v1.0) pkg> activate . -(benchmark) pkg> ^C +(v1.0) pkg> add BenchmarkTools +(v1.0) pkg> *press backspace* julia> using BenchmarkTools julia> include("stl.jl") julia> @btime STL.parse("nist.stl") - 211.641 μs (9 allocations: 347.06 KiB) + 140.942 μs (14 allocations: 347.30 KiB) ``` From the `python` directory: @@ -67,4 +67,4 @@ $ python benchmark.py ## Disclaimer I am neither a C++ nor Julia expert. Please let me know if I biased the results -by implementing something obviously inefficiently. \ No newline at end of file +by implementing something obviously inefficiently. diff --git a/julia/stl.jl b/julia/stl.jl index 1ae929d..047969a 100644 --- a/julia/stl.jl +++ b/julia/stl.jl @@ -15,18 +15,19 @@ struct Triangle v3::Vertex end -function parse(path::AbstractString) - open(path) do stl - skip(stl, 80) # skip header - trianglecount = read(stl, UInt32) - ref = Ref{Triangle}() - triangles = map(1:trianglecount) do i - read!(stl, ref) - skip(stl, 2) # skip attribute byte count - ref[] +function parse(path) + open(path; lock = false) do io + skip(io, 80) # skip header + triangle_count = read(io, UInt32) + triangles = Vector{Triangle}(undef, triangle_count) # preallocate memory for triangles + dest = Base.unsafe_convert(Ptr{Triangle}, triangles) # destination pointer + unsafe_read(io, dest, sizeof(Triangle)) # copying first triangle + for _ in 2:triangle_count + skip(io, 2) + dest += 48 # moving to the next trianlge in dest + unsafe_read(io, dest, sizeof(Triangle)) end - @assert eof(stl) - return triangles + triangles end end From 7668d72f715b37dceb23792b5b721a5e978aad82 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Thu, 29 Apr 2021 18:58:50 +0300 Subject: [PATCH 02/18] Upd julia version --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe26787..3fea711 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ From the `julia` directory: ```console $ julia -O3 julia> ] -(v1.0) pkg> add BenchmarkTools -(v1.0) pkg> *press backspace* +(v1.5) pkg> add BenchmarkTools +(v1.5) pkg> *press backspace* julia> using BenchmarkTools julia> include("stl.jl") julia> @btime STL.parse("nist.stl") From b94c699fe531dd01ccf368dc9a8b6ed40bcc2da5 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Thu, 29 Apr 2021 19:12:27 +0300 Subject: [PATCH 03/18] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3fea711..d4d7998 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ For more information: [STL Benchmark Comparison: C++ vs. Julia](https://aaronang ## Getting Started ```console -$ git clone --recurse-submodules git@github.com:aaronang/stl-benchmark.git +$ git clone --recurse-submodules git@github.com:XInvisib1eX/stl-benchmark.git ``` From the `cpp` directory: From dc30aee5fb1d7a4591661449066aa59d51d5cf33 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Thu, 13 May 2021 15:40:31 +0300 Subject: [PATCH 04/18] Update README.md --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d4d7998..3e112b6 100644 --- a/README.md +++ b/README.md @@ -7,19 +7,24 @@ For more information: [STL Benchmark Comparison: C++ vs. Julia](https://aaronang ## Getting Started ```console -$ git clone --recurse-submodules git@github.com:XInvisib1eX/stl-benchmark.git +git clone --recurse-submodules git@github.com:XInvisib1eX/stl-benchmark.git ``` From the `cpp` directory: ```console -$ export CC=/usr/bin/clang -$ export CXX=/usr/bin/clang++ -$ mkdir build -$ cd build -$ cmake -DCMAKE_BUILD_TYPE=Release .. -$ make -j -$ ./stl_benchmark +export CC=/usr/bin/clang +export CXX=/usr/bin/clang++ +mkdir build +cd build +cmake -DCMAKE_BUILD_TYPE=Release .. +make -j +./stl_benchmark +``` + +Sample output + +```console 2018-10-06 12:35:31 Running ./stl_benchmark Run on (8 X 2300 MHz CPU s) From f9083c840193ac7ed6516f85ef48add88c6d61ff Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:47:03 +0300 Subject: [PATCH 05/18] Update README.md --- README.md | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3e112b6..11163bd 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,11 @@ From the `julia` directory: ```console $ julia -O3 julia> ] -(v1.5) pkg> add BenchmarkTools -(v1.5) pkg> *press backspace* +(v1.6) pkg> add BenchmarkTools julia> using BenchmarkTools julia> include("stl.jl") -julia> @btime STL.parse("nist.stl") - 140.942 μs (14 allocations: 347.30 KiB) +julia> @btime parse_to_stack("nist.stl") + 110.646 μs (14 allocations: 347.30 KiB) ``` From the `python` directory: @@ -66,10 +65,5 @@ $ python benchmark.py | Language | Time | |----------|------------| | C++ | 409.210 μs | -| Julia | 211.641 μs | +| Julia | 110.646 μs | | Python | 25150.9 μs | - -## Disclaimer - -I am neither a C++ nor Julia expert. Please let me know if I biased the results -by implementing something obviously inefficiently. From bdd7d74b57a7946151760949c7b61666c1f0fd4d Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:54:18 +0300 Subject: [PATCH 06/18] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 11163bd..07fe3c9 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ CPU Caches: -------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------- -ParseStl 429234 ns 409210 ns 1729 +ParseStl 233049 ns 233047 ns 2960 ``` From the `julia` directory: @@ -64,6 +64,6 @@ $ python benchmark.py | Language | Time | |----------|------------| -| C++ | 409.210 μs | +| C++ | 233.049 μs | | Julia | 110.646 μs | | Python | 25150.9 μs | From 416cd9ed27efff507514d1b17cb1a2c532a4760a Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:59:57 +0300 Subject: [PATCH 07/18] wrote parser with malloc --- julia/stl.jl | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/julia/stl.jl b/julia/stl.jl index 047969a..dd623d3 100644 --- a/julia/stl.jl +++ b/julia/stl.jl @@ -1,5 +1,7 @@ module STL +export parse_to_stack, parse_to_heap + struct Vertex x::Float32 y::Float32 @@ -15,22 +17,36 @@ struct Triangle v3::Vertex end -function parse(path) +function parse_to_stack(path) open(path; lock = false) do io skip(io, 80) # skip header - triangle_count = read(io, UInt32) + triangle_count = read(io, UInt32) triangles = Vector{Triangle}(undef, triangle_count) # preallocate memory for triangles dest = Base.unsafe_convert(Ptr{Triangle}, triangles) # destination pointer unsafe_read(io, dest, sizeof(Triangle)) # copying first triangle for _ in 2:triangle_count - skip(io, 2) - dest += 48 # moving to the next trianlge in dest - unsafe_read(io, dest, sizeof(Triangle)) + skip(io, 2) + dest += 48 # moving to the next trianlge in dest + unsafe_read(io, dest, sizeof(Triangle)) end - triangles + triangles end end -export parse +function parse_to_heap(path) + open(path; lock = false) do io + skip(io, 80) + triangle_count = read(io, UInt32) + triangles = convert(Ptr{Triangle}, Base.Libc.malloc(48triangle_count)) + dest = triangles + unsafe_read(io, dest, sizeof(Triangle)) # copying first triangle + for _ in 2:triangle_count + skip(io, 2) + dest += 48 # moving to the next trianlge in dest + unsafe_read(io, dest, sizeof(Triangle)) + end + Base.unsafe_wrap(Array, triangles, triangle_count; own = true) + end +end -end # module +end #module STL From 1bd23ab23f04e8a4fbdb0dffc88451fc2ec4adb5 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:00:32 +0300 Subject: [PATCH 08/18] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 07fe3c9..0703327 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ julia> ] (v1.6) pkg> add BenchmarkTools julia> using BenchmarkTools julia> include("stl.jl") +julia> using Main.STL julia> @btime parse_to_stack("nist.stl") 110.646 μs (14 allocations: 347.30 KiB) ``` From 1ab377fdf92070bb297ad5e1ec1cbd19c57e6174 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:08:39 +0300 Subject: [PATCH 09/18] Update README.md --- README.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0703327..4061c14 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,17 @@ make -j Sample output ```console -2018-10-06 12:35:31 Running ./stl_benchmark -Run on (8 X 2300 MHz CPU s) +Run on (12 X 4500 MHz CPU s) CPU Caches: - L1 Data 32K (x4) - L1 Instruction 32K (x4) - L2 Unified 262K (x4) - L3 Unified 6291K (x1) + L1 Data 32K (x6) + L1 Instruction 32K (x6) + L2 Unified 256K (x6) + L3 Unified 12288K (x1) -------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------- -ParseStl 233049 ns 233047 ns 2960 +ParseStl 237056 ns 236744 ns 2912 ``` From the `julia` directory: @@ -65,6 +64,6 @@ $ python benchmark.py | Language | Time | |----------|------------| -| C++ | 233.049 μs | +| C++ | 237.056 μs | | Julia | 110.646 μs | | Python | 25150.9 μs | From e3f95bb1ad6080c37ccbcc883021e29900397197 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:12:10 +0300 Subject: [PATCH 10/18] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4061c14..7642d5a 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ From the `python` directory: ```console $ python benchmark.py -25150.930999999986 μs +13513.513993530069 μs μs ``` > Note: Python 3.7.0 is required. @@ -66,4 +66,4 @@ $ python benchmark.py |----------|------------| | C++ | 237.056 μs | | Julia | 110.646 μs | -| Python | 25150.9 μs | +| Python | 13513.5 μs | From 6e68bcd269e3eaa3f2839072cfff4fa1f79da682 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:16:16 +0300 Subject: [PATCH 11/18] Update main.cpp --- cpp/main.cpp | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/cpp/main.cpp b/cpp/main.cpp index c4d858e..aac5fea 100644 --- a/cpp/main.cpp +++ b/cpp/main.cpp @@ -1,5 +1,6 @@ -#include +#include #include + #include "benchmark/benchmark.h" struct point { @@ -7,6 +8,7 @@ struct point { float y; float z; + point() = default; point(float xp, float yp, float zp) : x(xp), y(yp), z(zp) {} }; @@ -16,46 +18,38 @@ struct triangle { point v2; point v3; + + triangle() = default; triangle(point normalp, point v1p, point v2p, point v3p) : normal(normalp), v1(v1p), v2(v2p), v3(v3p) {} }; -std::vector parse_stl(const std::string &stl_path) { - std::ifstream stl_file(stl_path.c_str(), std::ios::in | std::ios::binary); - - // skip header - char header_info[80]; - stl_file.read(header_info, 80); - - char n_triangles[4]; - stl_file.read(n_triangles, 4); - auto *r = (unsigned int *) n_triangles; - unsigned int num_triangles = *r; +std::vector parse_stl(const char* stl_path) { + using std::fread; + auto std_file = std::fopen(stl_path, "rb"); + + char dummy[80]; + fread(dummy, 1, 80, std_file); - std::vector triangles; - triangles.reserve(num_triangles); + std::uint32_t n_triangles; + fread(&n_triangles, 4, 1, std_file); - for (unsigned int i = 0; i < num_triangles; i++) { - float fs[12]; - stl_file.read((char *) fs, 48); - point normal{fs[0], fs[1], fs[2]}; - point v1{fs[3], fs[4], fs[5]}; - point v2{fs[6], fs[7], fs[8]}; - point v3{fs[9], fs[10], fs[11]}; - triangles.emplace_back(normal, v1, v2, v3); - - // skip attribute byte count + std::vector triangles(n_triangles); + + for (auto& tr : triangles){ + fread(&tr, 48, 1, std_file); char dummy[2]; - stl_file.read(dummy, 2); + fread(dummy, 1, 2, std_file); } - - assert(stl_file.peek() == EOF); - + + std::fclose(std_file); + return triangles; } +constexpr auto stl = "nist.stl"; + static void ParseStl(benchmark::State& state) { - std::string stl = "nist.stl"; for (auto _ : state) { parse_stl(stl); } From 5d5fc3c4ddf12fffc84789684e4b1816063ba19d Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:44:01 +0300 Subject: [PATCH 12/18] Update naming --- julia/stl.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/julia/stl.jl b/julia/stl.jl index dd623d3..08b3203 100644 --- a/julia/stl.jl +++ b/julia/stl.jl @@ -1,6 +1,6 @@ module STL -export parse_to_stack, parse_to_heap +export parse, parse_malloc struct Vertex x::Float32 @@ -17,7 +17,7 @@ struct Triangle v3::Vertex end -function parse_to_stack(path) +function parse(path) open(path; lock = false) do io skip(io, 80) # skip header triangle_count = read(io, UInt32) @@ -33,7 +33,7 @@ function parse_to_stack(path) end end -function parse_to_heap(path) +function parse_malloc(path) open(path; lock = false) do io skip(io, 80) triangle_count = read(io, UInt32) From 3a992862c1a4ff0b5c8c5d5dabb555f3478f066e Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:44:29 +0300 Subject: [PATCH 13/18] Update naming --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7642d5a..8ae2b83 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ julia> ] julia> using BenchmarkTools julia> include("stl.jl") julia> using Main.STL -julia> @btime parse_to_stack("nist.stl") +julia> @btime parse("nist.stl") 110.646 μs (14 allocations: 347.30 KiB) ``` From f8e62d700b60ba698cecf836976e42e4ccea6528 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:46:36 +0300 Subject: [PATCH 14/18] Update stl.jl --- julia/stl.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/julia/stl.jl b/julia/stl.jl index 08b3203..049640c 100644 --- a/julia/stl.jl +++ b/julia/stl.jl @@ -26,7 +26,7 @@ function parse(path) unsafe_read(io, dest, sizeof(Triangle)) # copying first triangle for _ in 2:triangle_count skip(io, 2) - dest += 48 # moving to the next trianlge in dest + dest += sizeof(Triangle) # moving to the next trianlge in dest unsafe_read(io, dest, sizeof(Triangle)) end triangles @@ -42,7 +42,7 @@ function parse_malloc(path) unsafe_read(io, dest, sizeof(Triangle)) # copying first triangle for _ in 2:triangle_count skip(io, 2) - dest += 48 # moving to the next trianlge in dest + dest += sizeof(Triangle) # moving to the next trianlge in dest unsafe_read(io, dest, sizeof(Triangle)) end Base.unsafe_wrap(Array, triangles, triangle_count; own = true) From 51e4e5065140bf005f74ab1fd0b7620447b0b5a6 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:48:34 +0300 Subject: [PATCH 15/18] Update stl.jl --- julia/stl.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/julia/stl.jl b/julia/stl.jl index 049640c..0696021 100644 --- a/julia/stl.jl +++ b/julia/stl.jl @@ -37,7 +37,7 @@ function parse_malloc(path) open(path; lock = false) do io skip(io, 80) triangle_count = read(io, UInt32) - triangles = convert(Ptr{Triangle}, Base.Libc.malloc(48triangle_count)) + triangles = convert(Ptr{Triangle}, Base.Libc.malloc(sizeof(Triangle)*triangle_count)) dest = triangles unsafe_read(io, dest, sizeof(Triangle)) # copying first triangle for _ in 2:triangle_count From 389d00ea311f05dbf072c367ed9ef5f61790e344 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:56:35 +0300 Subject: [PATCH 16/18] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 8ae2b83..2037b92 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,7 @@ julia> ] (v1.6) pkg> add BenchmarkTools julia> using BenchmarkTools julia> include("stl.jl") -julia> using Main.STL -julia> @btime parse("nist.stl") +julia> @btime STL.parse("nist.stl") 110.646 μs (14 allocations: 347.30 KiB) ``` From ed7f2b72501e586863508670d667c2d2b284146c Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+XInvisib1eX@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:58:18 +0300 Subject: [PATCH 17/18] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2037b92..97a9feb 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ For more information: [STL Benchmark Comparison: C++ vs. Julia](https://aaronang git clone --recurse-submodules git@github.com:XInvisib1eX/stl-benchmark.git ``` +NB! to run this you need to place `#include ` into cpp/benchmark/src/benchmark_register.h + From the `cpp` directory: ```console From 34f5b2c8851f5e778e6e8034d17c0a87c6b39720 Mon Sep 17 00:00:00 2001 From: Pavel Dydyshko <43893794+pddshk@users.noreply.github.com> Date: Sun, 15 Jan 2023 04:44:24 +0300 Subject: [PATCH 18/18] Fix profile name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 97a9feb..ee2c342 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ For more information: [STL Benchmark Comparison: C++ vs. Julia](https://aaronang ## Getting Started ```console -git clone --recurse-submodules git@github.com:XInvisib1eX/stl-benchmark.git +git clone --recurse-submodules git@github.com:pddshk/stl-benchmark.git ``` NB! to run this you need to place `#include ` into cpp/benchmark/src/benchmark_register.h